Skip to content

Instantly share code, notes, and snippets.

@eman41
Last active December 16, 2015 17:39
Show Gist options
  • Save eman41/5471678 to your computer and use it in GitHub Desktop.
Save eman41/5471678 to your computer and use it in GitHub Desktop.
A ruby snippet found on SO for doing a find and replace in a text file. Slightly modified for a recent use case.
# Inspired this question/answer (up-vote him!):
# http://stackoverflow.com/a/1274631/1598965
file_names = ['foo.txt', 'bar.txt']
file_names.each do |file_name|
text = File.read(file_name).gsub(/search_regexp/, "replace string")
# This will overwrite the existing file with the above gsub output!
File.open(file_name, "w+") {|file| file.puts text}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment