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

Re: change all files in directory

Thread Previous
From:
Jenda Krynicky
Date:
February 1, 2002 07:30
Subject:
Re: change all files in directory
Message ID:
3C5AC2C8.31320.3680AB9@localhost
From:           	Booher Timothy B 1stLt AFRL/MNAC <timothy.booher@eglin.af.mil>

> 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/;}
> }


%replace = (
	satellite => 'target',
	Satellite => 'Target',
	SATELLITE => 'TARGET',
);
$re = join('|',keys %replace);
while (<IN>) {
	s/($re)/$replace{$1}/go;
}

Not sure if it'll be any quicker. If you keep your code do not forget 
to add the /g option, otherwise you'd only replace the first 
occurence on each line.

Jenda

=========== Jenda@Krynicky.cz == http://Jenda.Krynicky.cz ==========
There is a reason for living. There must be. I've seen it somewhere.
It's just that in the mess on my table ... and in my brain.
I can't find it.
					--- me

Thread Previous


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