Home » Perl

Simple recursive search and replace with perl

Submitted by on June 25, 2006 – 2:44 am 1,016 views No Comment

I recently needed to do a mass search and replace for some content on my website. I vaguely recalled doing this in the past with find, sed, and/or xargs so decided to ‘Google’. My memory was jogged when I found this example:

find *.ext -type f -exec sed -i ‘s/OldText/NewText/’ {} \;

and I was very pleased to find that you can also do this with perl:

(Current dir only, making a backup with an appended ~)
perl -pi~ -e ‘s/OldText/NewText/’ *.ext

(Recursive, editing the file directly)
find *.ext | xargs perl -p -i -e s/OldText/NewText/g

Post to Twitter Post to Delicious

Leave a comment!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.