Easy find and replace with perl

If you need an easy way to find and replace a string in one or more text files (php script, bash script, perl script or just text) using only a command, may be that the following is the easiest way:

find . -name ‘[PATTERN]’ |xargs perl -pi -e ‘s/[NEEDLE]/[REPLACESTR]/g’

Example: if you are in the folder where you usually store you alert script, that send email on certain events, and you want to replace the previous email with the new one you can type

find . -name ‘*.php’ |xargs perl -pi -e ‘s/oldemail@domain.tld/newemail@domain.tld/g’

DONE!

Of course, you are supposed to have perl installed!
See man find for a complete list of search options.

Leave a Reply

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