我正在读取文件--list.txt并检查list.txt中的字符串是否存在于目录中的多个文件中。 (E:\工作...)。下面是我的代码,它按预期工作。
pool = ''
rows = Array[]
id_files = Dir.glob("E:\work'*-access.txt")
value=File.open('E:\nando\list.txt').read
value.each_line do |line|
line.chomp!
id_files.each do |file_name|
text = File.read(file_name)
new_content = text.gsub( /#{Regexp.escape(line)}\,\s/, '').gsub( /#{Regexp.escape(line)}/, '' )
unless text == new_content
text.each_line do |li|
if li.match(/#{Regexp.escape(line)}/) then
rows << li # I didn't complete the array part
puts rows[0]
puts rows[1]
pool = li.split(" ")[0]
end
end
File.open('E:\Removed_users.txt', 'a') { |log|
log.puts "Removed from: #{file_name}"
log.puts "Removed id : #{line}"
log.puts "Removed from row :#{pool}"
log.puts "*" * 50
}
File.open(file_name, "w") { |file| file.puts new_content }
end
end
end
我正在尝试增强代码以下面的格式打印到日志文件.Eg表示字符串 - 类\ 234ha和cap \ 7y6t5来自目录中的一个文件。
Removed class\234ha from row = id and dept
Removed cap\7y6t5 from row = id
E:\工作\样品access.txt
CDA created on September 20th 1999
Owner: Edward Jenner
Access IDs,
id = class\234ha, class\poi23, class\opiuj, cap\7y6t5
dept = sub\6985de, ret\oiu87, class\234ha
rec = ret\oiu87
我只能打印一次.i.e。对于类\ 234ha我正在输出
Removed from: E:\work\sample-access.txt
Removed user : class\234ha
Removed from row = id
理想情况下应该是
Removed from: E:\work\sample-access.txt
Removed user : class\234ha
Removed from row = id and dept
我尝试使用数组选项,但我没有得到预期的结果,我不想完全重写代码或逻辑。 Ruby专家,任何建议都会非常有用。感谢。
答案 0 :(得分:0)
可能最简单的解决方案(对您的问题的改动很小)是
# ... your code
pool = [] # define array for current match
text.each_line do |li|
# ... your code
pool << li.split(" ")[0] # add to array
# ... your code
log.puts "Removed from row :#{pool.join(' and ')}" # list all the ids joined by and
# ... your code
我希望我没有忽视某些事情