[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

String substitution in a number of files

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
0
micro_learner
Asked:
micro_learner
  • 2
3 Solutions
 
Kim RyanIT ConsultantCommented:
This question come up often and has been answered before. Look at
http://www.experts-exchange.com/Programming/Programming_Languages/Perl/Q_20431429.html
0
 
ozoCommented:
use File::Find;
find(sub{
    return unless -f && -T;
    local @ARGV=($_);
    local $^I=""; #or ".bak" or "/backup/*" if you want a backup
    while( <> ){
        s/\bBIO_HOME\b/BIOLOGY_HOME/g;
        print
    }
},".");
0
 
TintinCommented:
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' {} \;
0
 
ozoCommented:
Or

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


0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now