Mass Search & Replace · Oct 7, 08:27 AM by Dylan Doxey
So, there you are with a bunch of files. You want to search and replace a given string in all of them, and you're not sure how to go about it. If you're a former windows user, such as myself, you might be inclined to reach for the nearest GUI editor, open all of the files simultaneously, and do a search and replace on all the files that way. However, I've yet to see a GUI based editor that runs on Linux which can handle this sort of task the way that UltraEdit would.
Perl to the rescue!
Here's an example of replacing "searchme" with "replacement" in all the PHP files in the current directory.
perl -pi -e 's{searchme}{replacement}xmsg' *.php
The beauty is that you just need to supply Perl with a list of files.
For example:
perl -pi -e 's{searchme}{replacement}xmsg' `find . -name 'holey*moley*'`
That does the search and replace on all files in the current, and subdirectories of the current directory where the filename contains "holey" and "moley" in that sequence.
May the force be with you.

Commenting is closed for this article.
