【发布时间】:2017-05-13 16:03:53
【问题描述】:
我有多个以下格式的文件
例如。文件:sample.txt
id = class\234ha, class\poi23, class\opiuj, cap\7y6t5
dept = sub\6985de, ret\oiu87, class\234ha
cko = cyr\hui87
我正在查找字符串并将其从多个文件中删除。例如,查找并删除字符串 - class\234ha。
我的代码工作正常,它删除了所有预期的字符串,但在删除预期或标记的字符串后,行尾有一个尾随逗号。
例如。删除字符串后的 sample.txt - class\234ha
id = class\poi23, class\opiuj, cap\7y6t5
dept = sub\6985de, ret\oiu87,
cko = cyr\hui87
我想删除 ret\oiu87, 之后的最后一个逗号only。对于多个文件,它应该是相同的。我不确定逗号后是否有换行符或空格。我怎样才能让它工作。提前致谢。
代码
pool = ''
svn_files = Dir.glob("E:\work'*-access.txt")
value=File.open('E:\nando\list.txt').read
value.each_line do |line|
line.chomp!
print "VALUE: #{line}\n"
svn_files.each do |file_name|
text = File.read(file_name)
replace = text.gsub( /#{Regexp.escape(line)}\,\s/, '').gsub( /#{Regexp.escape(line)}/, '' )
unless text == replace
text.each_line do |li|
if li.match(/#{Regexp.escape(line)}/) then
#puts "Its matching"
pool = li.split(" ")[0]
end
end
File.open('E:\Removed_users.txt', 'a') { |log|
log.puts "Removed from: #{file_name}"
log.puts "Removed user : #{line}"
log.puts "Removed from row :#{pool}"
log.puts "*" * 50
}
File.open(file_name, "w") { |file| file.puts replace }
end
end
end
【问题讨论】:
标签: ruby