develooper Front page | perl.beginners | Postings from February 2002

RE: change all files in directory

Thread Previous | Thread Next
From:
Craig Williams
Date:
February 1, 2002 07:38
Subject:
RE: change all files in directory
Message ID:
E94E9A31F8EDD311BA4B00508B6493BB52455C@epsnt1.epshq.eps.rwk.com
Somewhat related question:

I want to ADD a file extension (there currently none)to all files that start
with "FOO"
so they will be named FOO.txt

Is there a one liner that can accomplish this?

-----Original Message-----
From: Hanson, Robert [mailto:RHanson@APTEGRITY.com]
Sent: Friday, February 01, 2002 9:30 AM
To: 'Booher Timothy B 1stLt AFRL/MNAC'; beginners@perl.org
Subject: RE: change all files in directory


Yeah, here is an easy solution, run it right at the command line...

(Make a backup of the files first!!)

perl -pi -e 's|satellite|target|' *.cpp *.hpp *.asc
perl -pi -e 's|Satellite|Target|' *.cpp *.hpp *.asc
perl -pi -e 's|SATELLITE|TARGET|' *.cpp *.hpp *.asc

Rob

-----Original Message-----
From: Booher Timothy B 1stLt AFRL/MNAC
[mailto:timothy.booher@eglin.af.mil]
Sent: Friday, February 01, 2002 10:22 AM
To: beginners@perl.org
Subject: change all files in directory


Hello, I am trying to change all files in a directory that contain the
strings Satellite satellite and SATELLITE that I need to change to target,
Target and TARGET. Because many of these are C++ source files I need to
preserve the case. I was thinking of the following script:
 
#!/usr/bin/perl -w
#UNTESTED
 
@FilesInDirectory = <*.cpp *.hpp *.asc>;
foreach $FileName (@FilesInDirectory) {
            open(IN, $FileName);
            while<IN> {
$_ =~ s/satellite/target/;
$_ =~ s/Satellite/Target/;
$_ =~ s/SATELLITE/TARGET/;}
}
 
but this just doesn't seem as efficient as it can be. I was trying to think
of regex that could do it all in one line but it seemed so much simpler to
do it in three.
 
Any thoughts,
 
Tim
 

-- 
To unsubscribe, e-mail: beginners-unsubscribe@perl.org
For additional commands, e-mail: beginners-help@perl.org

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About