4
我有这个用Ruby编写的小程序。我在这里发现了一段很好的代码,用于查找和替换文件中的某些内容,但似乎并不奏效。 这里是代码:查找并替换Ruby中的文件
#!/usr/bin/env ruby
DOC = "test.txt"
FIND = /,,^M/
SEP = "\n"
#make substitution
File.read(DOC).gsub(FIND, SEP)
#Check if the line already exist
unique_lines = File.readlines(DOC).uniq
#Save the result in a new file
File.open('test2.txt', 'w') { |f| f.puts(unique_lines) }
谢谢大家!