I have multiple files of below format
Eg. file: sample.txt
id = class\234ha, class\poi23, class\opiuj, cap\7y6t5
dept = sub\6985de, ret\oiu87, class\234ha
cko = cyr\hui87
I'm finding string and removing it from multiple files.Like, find and remove string - class\234ha.
My code is working fine and its removing all the intended strings but there is a trailing comma at the end of the line after the intended or marked string is deleted.
Eg. sample.txt after removal of string - class\234ha
id = class\poi23, class\opiuj, cap\7y6t5
dept = sub\6985de, ret\oiu87,
cko = cyr\hui87
I want to remove the last comma only after ret\oiu87, .it should be the same for multiple files. I'm not sure whether there is a new line character or space after the comma. How can I make it work. Thanks in advance.
code
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