0

I just started learning Ruby.

I need to replace some lines in a file, which are having a specific string in it.

How to append the file using Ruby?

I have tried different approaches, including:

file_names.each do |file_name|
    text = File.read(file_name)
    puts text.gsub(/search_regexp/, "replace string")
end
Sully
  • 14,672
  • 5
  • 54
  • 79
  • Are you trying to replace the filename? File.read will get you the file contents. You may want to loop each line then? Read http://stackoverflow.com/questions/15991858/ruby-regex-gsub-a-line-in-a-text-file – Sully Jun 04 '14 at 09:19

1 Answers1

1

To make the replacing permanent you would have to write the file back to disk

read = File.read(file).gsub(/search_regexp/, "replace string")
File.write(file, read)
peter
  • 41,770
  • 5
  • 64
  • 108