继续下面的链接中的stackoverflow问题:
ruby match string or space/tab at the beginning of a line and insert uniq lines to a file
我有这些文件 - ./files_tmp/60416.log
:
AAAAAA555
AAAAAA555
BBBBBB
CCCCC
AAAAAA434343
AAAAAA434343
./files_tmp/60417.log
AAAAAA55544
AAAAAA55544
BBBBBB
CCCCC
AAAAAA434343
AAAAAA434343
我有这段代码:
files = Dir["./files_tmp/*.log"]
files.each do |file_name|
puts file_name if !File.directory? file_name
Tempfile.open do |temp|
File.open(file_name) do |input|
input.each_line do |line|
if line.match(/AAAAAA/) || (line.match(/^\t/) and tabs)
puts "found a line #{line}"
temp.write(line.lstrip!)
end
end
end
File.open("./temp.log", "a") do |file|
temp.rewind
file.write(temp.readlines.uniq.join(""))
end
end
end
puts "found a line #{line}"
的结果如下,但我预计会打印仅行AAAAAA
./files_tmp/60416.log
found a line AAAAAA555
found a line AAAAAA555
found a line BBBBBB
found a line CCCCC
found a line AAAAAA434343
found a line AAAAAA434343
./files_tmp/60417.log
found a line AAAAAA55544
found a line AAAAAA55544
found a line BBBBBB
found a line CCCCC
found a line AAAAAA434343
found a line AAAAAA434343

./temp.log
中看到重复的行,而不是所有行AAAAAA
AAAAAA434343
AAAAAA434343

我期望:
AAAAAA555
AAAAAA434343
AAAAAA55544

我想知道为什么?
我使用file.write(temp.readlines.uniq.join(""))
代替file.write(temp.readlines.uniq)
,因为结果将是:
["AAAAAA434343\n"]
了解rewind
目的是什么,它有什么用呢?
感谢您的帮助!
答案 0 :(得分:1)
您不需要弄乱Tempfile
。只需收集您想要的内容,然后将所有内容写入目标文件:
result = Dir["./files_tmp/*.log"].each_with_object([]) do |file_name, lines|
next if File.directory? file_name # skip dirs
File.readlines(file_name) do |line|
next unless line =~ /AAAAAA/
puts "found a line #{line}"
lines |= [line.lstrip!] # append if and only it’s uniq
end
end
File.write("./temp.log", result.join($/)) # join with OS-aware line sep