Tuesday, October 24th, 2006...8:35 am

Perl One Liner – Search Replace String in File

Jump to Comments

This is one of the most useful perl one-liners. Search and Replace a string in a text file.

perl -0777 -i -pe 's/patterntomatch/replacewith/g' inputfile

Let's break this down

-0777: treat file as a single line, this way we don't have to worry about ranges on our regex.

-i: operate on file in place. super dangerous and fun. If you're a big wuss you could use -i.bak to automatically create a backup file.

-pe: loop over and print output.

Leave a Reply