Perl Just Like sed

Mar 15, 2007

I came across a post today in which someone mentioned that they used Perl for text replacement and, despite the simplicity of the solution, I hadn’t ever thought of it. By simply using three options with the perl command, you can run a quick script to do regex replacement in a file. The -p option tells Perl to treat your script as a loop, reading in from the diamond (<>) operator. -i makes it do in-place editing of files and -e allows you to script from the command line. For example:

~$ perl -p -i -e "s/foo/bar/g" baz.txt

The above command will replace ‘foo’ with 'bar’ throughout the file 'baz.txt’