You can create an array with all the words or strings you want to find and then delete/replace:
strings_to_delete = ['aaa', 'domain1esrt', 'delete_me']
Then to read the file and use map
to create an array with all the lines who doesn't match with none of the elements in the array created before:
# read the file 'text.txt'
lines = File.open('text.txt', 'r').map do|line|
# unless the line matches with some value on the strings_to_delete array
line unless strings_to_delete.any? do |word|
word == line.strip
end
# then remove the nil elements
end.reject(&:nil?)
And then open the file again but this time to write on it, all the lines which didn't match with the values in the strings_to_delete
array:
File.open('text.txt', 'w') do |line|
lines.each do |element|
line.write element
end
end
The txt
file looks like:
aaa
domain1esrt
domain2345p
yrtfj
tkpdp
....
....
delete_me
I don't know how it'll work with a bigger file, anyways, I hope it helps.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…