Skip to content

Latest commit

 

History

History
24 lines (15 loc) · 1.11 KB

File metadata and controls

24 lines (15 loc) · 1.11 KB

replacing text in a file with ruby

Long, long ago I learned the Best way to replace text in a hurry with lots of files was to have perl hanging around. It had this 'great' command line function where you could feed it a regex, replacement string, path, and file matcher and it would rip through your directory like a boss. This was invaluable when I was a java developer and the tools sucked and you wanted to move classes to a new package.

perl -pi.bak  -e 's/old/new/g;' *.config

Lo' and behold, ruby has the same syntax:

ruby -pi.bak -e "gsub(/old/, 'new')" *.config

-p means keep looping while there's something in $_. In our case that's text in a file and files in a list.

-i means edit this file in place

-i.bak means edit in place and make a backup with a bak extension (optional if you're brave!)

-e means run the next line. In this context that means run this command over and over until there's no more input.

"gsub(/old/, 'new')" means replace old with new. Google "ruby gsub" if you need more info.

*.config means do this for all the *.config files in this folder.