Adam Fields (weblog)

This blog is largely deprecated, but is being preserved here for historical interest. Check out my index page at for more up to date info. My main trade is technology strategy, process/project management, and performance optimization consulting, with a focus on enterprise and open source CMS and related technologies. More information. I write periodic long pieces here, shorter stuff goes on twitter or


Search and Replace with a perl one-liner and find

Filed under: — adam @ 11:08 am

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

Again, if you want more matches, you can add more -o -name arugments (-o is the find syntax for “or”).

Comments are closed.

Powered by WordPress