Search and Replace with a perl one-liner and find
If you’re on a linux/unix or Windows machine with perl, you can do search and replace in multiple files with a perl one-liner:
perl -p -i.bak -e 's|oldtext|newtext|g;'
If you want to do multiple substitutions, you can add more -e arguments. The example above also makes a backup copy of each file with a .bak extension. Just use -i if you don’t want that.
If you want to do multiple recursive directories, and you have find, you can do this:
find /path/to/top/dir -type f \( -name "*.htm" -o -name "*.html" \) -print0 | xargs -0 perl -p -i.bak -e
's|oldtext|newtext|g;'
Again, if you want more matches, you can add more -o -name arugments (-o is the find syntax for “or”).
Comments Off