Debian Admin

Debian/Ubuntu Linux System Administration Tutorials,Howtos,Tips

  • RSS Subscribe

    subscribe to the Debian Admin RSS feed
  • Sponsors



  • Categories

  • Sponsors

  • Support DebianAdmin

    Amount $:
    Website(Optional):


  • Meta

  • Archives



Howto Replace multiple file text string in Linux

Posted by Admin on August 13th, 2007

If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!

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.

  • Share/Bookmark

Random Posts

6 Responses to “Howto Replace multiple file text string in Linux”

  1. James Says:

    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.

  2. daniel Says:

    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?

  3. Michael Says:

    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!

  4. Eol Says:

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

  5. afjal Says:

    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

  6. J Says:

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

Leave a Reply

XHTML: 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>