perl -p -i.bak -e 's/find/replace/g' *
If youíve used Perl at all you are probably familiar with the simple oneliner to do a search and replace on a given string:
perl -p -i -e 's/oldstring/newstring/g' *
This will replace all occurrences of oldstring with newstring in all of the files in your current directory. Being able to do this quickly and easily is absolutely awesome, but what if you want to do this recursively across the current directory and all directories below that? Iím sure that there are plenty of ways to do this, the one below is the method that seems the easiest:
perl -p -i -e 's/oldstring/newstring/g' `find ./ -name *.html`
If you wanted to be more precise you could replace find with grep and only perform the search and replace on files that you know already contain oldstring, like this:
perl -p -i -e 's/oldstring/newstring/g' `grep -ril oldstring *`
This is one of those things that I donít have to do very often, but when I do, this Perl oneliner is a real life saver.
No related posts.