Linux: How do I mass find and replace text in files under linux using perl?


Few friends have asked me how to do mass find and replace text in files under linux. There are quite a bit of options in linux to achieve mass replacing of text in files. If you are doing it file by file, you can achieve that in vi by opening, running replace and closing and going to next file. But sometimes that can be very tedious and you would rather do mass replacement on all files containing certain extension. We can do this by using sed or perl. Since most people are familiar with perl (at least most system admins and programmers), I will show you a perl way of doing it which you can use with sed as well if you wish. First step is to get perl to do what we want on one file

perl -w -i -p -e "s/search_text/replace_text/g" filename

-w turns warnings on
-i makes Perl operate on files in-place (if you would like to make backups, use -i.bak, this will save filename.bak files)
-p loops over the whole input
-e specifies Perl expression
filename works on one file at a time

Once we get the results we want, we can now pass it multiple files by doing something like:

perl -w -i -p -e "s/search_text/replace_text/g" *.php

This will search and replace within all php files in the directory you are in.

Now, let us say you want to go through your whole web directory and replace every place where you have “Perl is good” to “Perl is great”, you would use following command:

find /www_root -name "*.php"|xargs perl -w -i -p -e "s/Perl is good/perl is great/g"

find will start at /www_root, look for filenames which have .php extension, xargs takes that filename and passes it to perl as an arguement.

————————————-
DISCLAIMER: Please be smart and use code found on internet carefully. Make backups often. And yeah.. last but not least.. I am not responsible for any damage caused by this posting. Use at your own risk.


14 Responses to “ Linux: How do I mass find and replace text in files under linux using perl? ”

  1. machiner
    December 5th, 2007 | 4:33 pm

    Handy perl bits like this rule. I, myself, am an ignorant schnub and rely on folks like you to share your hard-earned knowledge.

    Thanks for it. I found it on Google quickly with the following search phrase:

    “find and replace text in directory +linux”

    Thanks,

    machiner

  2. February 2nd, 2008 | 6:12 am

    Here’s a time saver:

    find /www_root -name “*.php” | …

    if you’re already in the desired path:

    ls -1 *.php |

  3. Jan klaassen
    February 26th, 2008 | 9:11 am

    Using google, i found several solutions for this problem. But this one was the shortest, so I used it and it worked!

  4. March 9th, 2008 | 12:05 am

    Great post!

  5. June 2nd, 2008 | 4:14 pm

    [...] and xargs takes the filename and passes it to perl.  To understand what perl command is doing, see this post. Print This Post (No Ratings Yet)  Loading … digg_url = [...]

  6. September 29th, 2008 | 11:55 am

    [...] You can read the original article here: Linux: How do I mass find and replace text in files under linux using perl? | Technology: Learn and …. [...]

  7. November 19th, 2008 | 5:49 pm

    THANK YOU !!

    My friend’s website was hacked and then its files were infested with malicious links.
    It’s wordpress – there seems to be a mass hack on websites running old version of wordpress.

    It would take HOURS to clean up those bloody links.

    With the trick mentioned here, it took only several seconds. Awesome.

    Thanks again !

  8. Balwinder Kumar
    April 2nd, 2009 | 7:06 am

    Excellent Help!!!Thanks a lot.

  9. Mike
    April 14th, 2009 | 4:42 pm

    Thanks to you, I was able to clean up a big mistake one of my rookie developers made in a matter of seconds.
    Thanks a ton.

  10. TDSii
    August 12th, 2009 | 9:01 am

    how is it possible to replace text with another that contains ” and / \ *

  11. August 12th, 2009 | 9:28 am

    You would escape them: \” and \/ \\ \*

    so putting \ in front should do the trick.

  12. September 23rd, 2009 | 12:26 am

    [...] this could be done through an sed script , but found a better one using perl from here. export OLD_TEXT=’<link rel=”stylesheet” type=”text/css” [...]

  13. February 6th, 2010 | 12:17 pm

    great article..

  14. August 25th, 2010 | 11:25 am

    What a great article!!! THANKS you saved me some work. I used to have a different method of doing this with Perl, and so I started trying to refresh my memory and found you. Great job! Proves that Perl’s mantra is alive and well in that there is “Always more than one way to do it”.

Leave a reply

*
To prove that you're not a bot, enter this code
Anti-Spam Image