Home > UNIX > Find and Replace Across Multiple Files

Find and Replace Across Multiple Files

Have you ever needed to change the same thing in a load of files and have had to sit there and go through each one in turn and make the same change?

This is a really useful and simple command:

perl -pi -w -e 's/from text/to text/g;' *

It simply goes through all files matching * and runs the regular expression, ‘s/from text/to text/g’ – which means replace all instances of “from text” with “to text”.  nifty!

Categories: UNIX Tags: , , ,
  1. March 16th, 2009 at 11:00 | #1

    Handy. I’ve previous just loaded all the files into a Textmate project, and then used a project wide search and replace, but that one liner is even easier.

  2. March 17th, 2009 at 20:29 | #2

    Yeah – it’s very nifty! – great for updating things like DNS records in bulk!

  3. Stephen Strike
    March 17th, 2009 at 22:40 | #3

    haha clearly what you worked out when moving servers then?

  4. March 17th, 2009 at 22:51 | #4

    Indeed!

  1. No trackbacks yet.