04 April, 2006

File search and replace with Perl

So, I want to search a bunch of files and do some text substitution. This is a fairly common task, but I can never remember how to do it. Here's how: In Perl:
perl -pi.bak -e 's/FindThis/AndReplaceItWithThis/g' *.fileExtension
This will do the substitution "in place," backing up the original file with a copy having the .bak extension. If you're brave, you can leave off the .bak extension and no backup will be made. I don't remember just now how to do the equivalent in Ruby, I'll come back and update this when I get a chance.

No comments: