Linux tip: How to replace text in multiple files
By Detector | 04 February 2009
If you have text you want to replace in multiple locations, there are several ways to do this. To replace the text Windows with Linux in all files in current directory called test[something] you can run this:
perl -i -pe ‘s/Windows/Linux/;’ test*
To replace the text Windows with Linux in all text files in current directory and down you can run this:
find . -name ‘*.txt’ -print | xargs perl -pi -e’s/Windows/Linux/ig’ *.txt
Or if you prefer this will also work, but only on regular files:
find -type f -name ‘*.txt’ -print0 | xargs –null perl -pi -e ‘s/Windows/Linux/’
Good luck! It saves a lot of time and it is excellent tip for every advanced Linux user!
Tags | Howto, Linux, Programming, Tips and Tricks