Quickie: Multiple File Search and Replace

Solution: perl -pi -w -e 's/searchregex/replaceregex/g;' *.fileextension

All Articles

Tags: perl, quickie, scripting

If you've ever run into the necessity to change the same data over multiple files you know how hard it is to go through each and every file and update it by hand or at best do an indiviual regex on each. Here's how you can make the changes all in on fell swoop.


perl -pi -w -e 's/searchregex/replaceregex/g;' *.fileextension


-w display any warnings

-i in place edit

-p loop over files

-e execute this line of code

Here's an example:


perl -pi -w -e 's/\x0D//g;' *.txt


This looks for those pesky ^M characters and replaces them with nothing.