Search String, Replace the entire raw with a new string

Hi,

I need a script which does the following.

  1. Searches for multiple strings in a file
  2. Replace the entire raws which contain those strings with a new
    string.

Thanks.

Below example only replaces the exact regex string, but I need it to
replace the entire row which contains that string.

file_names = [‘file.txt’]

file_names.each do |file_name|
text = File.read(file_name)
new_contents = text.gsub(/regex_string/, “new_string”)
File.open(file_name, “w”) {|file| file.puts new_contents }

end

You could look this up easily enough in the Regex documentation:
“^” is the start of a line
“$” is the end of a line
“.*” is any set of characters excluding newlines, except in multiline
mode.

/^.regex_string.$/

Joel P. wrote in post #1179165:

You could look this up easily enough in the Regex documentation:
“^” is the start of a line
“$” is the end of a line
“.*” is any set of characters excluding newlines, except in multiline
mode.

/^.regex_string.$/

Thank you very much. The issue was already resolved the same way.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs