Aug 132007
 

Sponsored Link

If you have a folder with a lot of files in a directory and with a specific string that you want to change you can do it in seconds using grep and perl command line

grep -R –files-with-matches ‘OLDSTRING’ . | sort | uniq | xargs perl -pi~ -e ’s/OLDSTRING/NEWSTRING/’

Where OLDSTRING is the string you want to find and replace with NEWSTRING.

 Posted by at 1:42 pm
  • James

    Not sure why you run uniq on it, grep is perfectly capable of only returning unique results… And use the short options please. This should do just fine:

    $ grep -rl OLDSTRING . | xargs perl -pi~ -e ‘s/OLDSTRING/NEWSTRING/’

    and if you want to do it sorted, files (this will even do unique results too!):

    $ grep -rl OLDSTRING . | sort -u | xargs perl -pi~ -e ‘s/OLDSTRING/NEWSTRING/’

    and for you old school sed peeps (sed args aren’t as obscure):

    $ grep -rl OLDSTRING . | xargs sed -i -e ‘s/OLDSTRING/NEWSTRING/’

    In all of these cases, you can replace the grep command with `find . -type f`. I’m guessing this hasn’t been performance tested. I’m also guessing for large files, just running the search and replace would be faster than scanning the file for matches, then scanning the file for matches AND replacing them.

  • http://wikimaas.org daniel

    In 1600 files want to replace the strings “text: ” by nothing, so i give the command

    $ grep -R -l ‘text: ‘ . | sort | uniq | xargs perl -pi~ -e ‘s/text: ‘//’

    and then Ubuntu Feisty says
    Unrecognized character \xE2 at -e line 1.
    xargs: perl: exited with status 255; aborting

    how to proceed?

  • Michael

    daniel:

    You’ll get that error if you copy and paste the line from your web browser because the single quote character that is pasted isn’t the same that you would type. Just delete the ‘s from the command and type them back in manually. Also, it appears that you might have an extra ‘ just before the //’ at the end of your command.

    I used James’ version, retyping the single quotes, and it ran perfectly, thanks!

  • Eol

    James’s solution works excellent for me
    Thanks a lot!

  • afjal

    1. $grep -R –files-with-matches ‘OLDSTRING’ . | sort | uniq | xargs perl -pi~ -e ’s/OLDSTRING/NEWSTRING/’

    2. $ grep -rl OLDSTRING . | xargs perl -pi~ -e ’s/OLDSTRING/NEWSTRING/’

    I tried both the options, but no use, i am on Linux4 63 bit.

    please let me know how to proceed !

    Thanks to ALL

  • J

    James’ solutions don’t work if your directories have space in their names.

  • david

    I want to search for start and end of some words and then replace them with : ..how do I make
    ‘OLDSTRING’ a search expression ? Can I do that..
    if not how do I search this and replace based on an expression ..

  • Tim

    I found this page via Google as I couldn’t remember how to do it.

    I’m using Centos 5 32bit though, putting in grep -Rl (then the rest of it) as mentioned gave me the error ‘Argument too long’.

    I had to replace the . with * as it’d just sit there doing nothing, anyway I found the following to work best for me, but it’s non recursive…

    grep -l OLDTEXT * | xargs perl -pi~ -e ‘s/OLDTEXT/NEWTEXT/’

  • Karth

    Hello,
    I need to search and replace multiple words in one pass of an input
    stream or string. For example, given the input:

    “The quick brown fox jumped over the lazy dog’s back”

    and given the replacements

    quick -> slow
    jump -> walk

    I’d like to get the string

    “The slow brown fox walked over the lazy dog’s back.”
    Which perl command do i use?(i used foreach but it is printing lines multiple times)..
    If you need i will produce code..

    thanks
    karth

  • Lena Schimmel

    Attention! None of the above commands (neither perl nor sed) seem to work correctly if OLDSTRING is found multiple times within the same line. In this case, only the first occurence within that line will be replaced and others remain unchanged. (Tested with perl 5.10.1 with debian patches and GNU sed 4.2.1)

    Is there a way to fix that? (For me it’s not a real problem, so I won’t come back here to check your answers, but maybe this well help someone else who stumbles across this page…)

  • http://www.robgroen.nl Rob Groen

    just place a g (global) behind the last slash in

    grep -l OLDTEXT * | xargs perl -pi~ -e ‘s/OLDTEXT/NEWTEXT/’

    so it becomes

    grep -l OLDTEXT * | xargs perl -pi~ -e ‘s/OLDTEXT/NEWTEXT/g’

  • Arno

    The rrep program lets you replace strings in multiple files and recursively in directories. It also supports regular expressions. The usage is similar to grep.
    Available in Debian, Ubuntu and at http://sourceforge.net/projects/rrep/