Debian Admin - Your way to Debian World

August 13, 2007

Howto Replace multiple file text string in Linux

by @ 1:42 pm. Filed under General

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.

Tags: ,

You may also be interested in...

3 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!

Leave a Reply

Subscribe RSS Feed

subscribe to the Debian Admin RSS feed

Internal links:

Sponsors:



Categories:

Support Debian Admin

Amount $:
Website(Optional):

Sponsors:

Archives:

WidgetBucks - Trend Watch - WidgetBucks.com

Related Links:


WidgetBucks - Trend Watch - WidgetBucks.com

Favourite Sites:

Wordpress Collection
Windows Reference
Ubuntu Geek
DebianHelp
All About Debian Tutorials
Power Electrical
Check Your IP Here
Debian,Ubuntu News
DebCentral
Tuxmachines
Capnkirby
Libervis
Nuxifield
Linux Horizon
Linux Appfinder
Debuntu
GNU/Linux For Everyone
Free Penguin
DebianAdmin is not related to the Debian Project.
This site is copyright © 2006,2007 Debian Admin
All Trademarks are the property of their respective owners.
The contents of this website may not be mirrored or archived without the express written permission of DebianAdmin Site Owner.

DISCLAIMER: All the information, troubleshooting methods, utilities offered in this website is provided AS-IS, without any warranties. Though I strive for perfection, and always test the validity and effectiveness of the troubleshooting content in various systems, I assume no responsibility for your use of these Fixes, Utilities and other troubleshooting advice. The author will not be liable for any special, incidental, consequential or indirect damages due to loss of data or any other reason. All use is completely at your own risk. Changes to the existing content and new additions are made to this website periodically, without notification.
Rodney's Kontera DynamiContext Plugin plugged in.