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.

1 Comment

  • Wonderful blog! Do you have any recommendations for aspiring writers? I'm planning to start my own website soon but I'm a little lost on everything. Would you suggest starting with a free platform like WordPress or go for a paid option? There are so many choices out there that I'm completely confused .. Any ideas? Thanks!

Leave a Reply