Perl’s “Search, replace, and commit back”


I showed the sed way to search for a pattern in a file, replace every occurrence of the pattern with something, and write all the changes back to the same file. I am, now, going to show you the Perl one-liner way of doing it.


$ perl -p -i -e 's/pattern/replace/g' file

Or, if you wish to backup the file first, and then make changes, here is the altered form:


# perl -p -i.bak -e 's/pattern/replace/g' file

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s