Perl in file pattern substitution


You want to substitute a pattern in a number of files, making backups and modifying in place on the fly.

And you can do this in Perl – with a one liner! 🙂



An amazing bit of Perl and so quick!

This one liner will replace all occurrences of a pattern, with your replace and even create backups of files edited.



You can even pass in a wildcard.

perl -pi'.bak' -e's/pattern/replace/g' filename
Here is a demo of replacing all occurrences of unix with UNIX, in all html files.

perl -pi'.bak' -e 's/unix/UNIX/g' *htm*



[tags]Perl infile pattern substitution, Perl Coding School[/tags]


If you have found my website useful, please consider buying me a coffee below 😉

Leave a Reply

Your email address will not be published. Required fields are marked *