String substitution in a number of files

Posted on 2004-11-24
Last Modified: 2010-03-05
Hello ..I have a bunch of txt files placed in a directory .The directory may have sub directories too.
I am trying to write a perl script that reads the txt files one by one and replaces the
string "BIO_HOME" to "BIOLOGY_HOME".

I have done a bit of perl programming before ..but I seem to have gone out of my depth here...especially
since I believe I have to use a bit of recursion here ( due to sub directories)...also I dont really understand
how to do the string substitution in the files .I have done string substitution opening individual files reading them and
then substituting them ..but I ma confused here

Could you please help me in here
Question by:micro_learner
    LVL 19

    Accepted Solution

    This question come up often and has been answered before. Look at
    LVL 84

    Assisted Solution

    use File::Find;
        return unless -f && -T;
        local @ARGV=($_);
        local $^I=""; #or ".bak" or "/backup/*" if you want a backup
        while( <> ){
    LVL 48

    Assisted Solution

    If you are on a Unix box, you can simply do:

    find /dir -name "*.txt" -exec perl -pi.bak -e 's/BIO_HOME/BIOLOGY_HOME/g' {} \;
    LVL 84

    Expert Comment


    find /dir -name "*.txt" -print0 | xargs -0 perl -pi.bak -e 's/BIO_HOME/BIOLOGY_HOME/g'


    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now