Entrepreneur Geek

Nirav Mehta on life, technology and future

Search and Replace Recursively using sed and grep

with 10 comments

I have done this so many times, still I find a new problem doing recursive search and replace each time! Here's one small shell script that puts the issues I had on the Mac doing search and replace on a directory recursively.

Save the file as rpl.sh, chmod it 755, and execute it like:

CODE:
  1. ./rpl.sh folderContainingFiles/ oldText newText

Does the job for me! This could be done in a single line, I got some ideas of using xargs etc, but all that did not work on my Mac (at least). So resorting to this.

CODE:
  1. #!/bin/sh
  2. grep -rl $2 $1 |
  3.  while read filename
  4.  do
  5.  (
  6.   echo $filename
  7.   sed "s/$2/$3/g;" $filename> $filename.xx
  8.   mv $filename.xx $filename
  9.  )
  10.  done

And yes, if you are going to use this, take a little time and do some argument checking before passing them around! Unless you think only a God user like you is going to use it!

Related posts:

  1. Finding number of lines in files recursively I wanted to find the number of lines from a...
  2. Recursively unlock files on Mac OS X I copied some songs from a CD and ended up...

Written by Nirav

December 29th, 2007 at 1:21 pm

Posted in Apple, GNU/Linux

 

10 Responses to 'Search and Replace Recursively using sed and grep'

Subscribe to comments with RSS or TrackBack to 'Search and Replace Recursively using sed and grep'.

  1. find containingFolder/ -type f -exec sed -i~ “s/oldText/newText/g” {} \;

    t3rmin4t0r

    31 Dec 07 at 6:34 am

  2. The exec part here: wouldn’t it fork a new process for every file? Is that a good practice? I thought otherwise!

    Nirav

    4 Jan 08 at 9:20 am

  3. Nirav

    4 Jan 08 at 9:20 am

  4. for i in `find` ; do sed “s/pattern/replacement/g” “$i” >> “$i.xx”; mv “$i.xx” “$i”; done

    I don’t know if this work in mac.

    Leonardo

    25 Jun 08 at 11:30 pm

  5. I haven’t ever seen a better file than this. It solved me days of work, you are gorgeous.

    I NEVER write comments to posts because I don’t have time, but this time I have 4 working-days of free credit that you have just saved me with this file.

    Thanks. ezequiel.

    ezequiel

    2 Sep 08 at 10:22 am

  6. For whole words, update line 7:
    sed “s/\b$2\b/$3/g;” $filename> $filename.xx

    Rookie

    5 Sep 08 at 6:32 pm

  7. sed -i ’s/oldText/newText/g’ `grep -ril ‘oldText’ *` works fine for me

    Trestini

    2 Mar 09 at 11:31 pm

  8. A small improvement:
    Change

    mv $filename.xx $filename

    by

    cp $filename.xx $filename
    rm $filename.xx

    and original file permissions will be preserved

    Antonio

    5 Dec 09 at 9:14 pm

  9. You can avoid the creation of a temporary file using -i option for sed:

    1 #!/bin/sh
    2 grep -rl $2 $1 |
    3 while read filename
    4 do
    5 (
    6 echo $filename
    7 sed -i “s/$2/$3/g;” $filename
    8 )
    9 done

    Regards,

    Hugo Magalhães

    12 Feb 10 at 9:41 pm

Leave a Reply