Perl in file pattern substitution

Problem

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! 🙂



Solution

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.



Example


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*



Reference

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



Leave a Reply

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