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

Re: change all files in directory

Thread Previous | Thread Next
From:
Brett W. McCoy
Date:
February 1, 2002 07:40
Subject:
Re: change all files in directory
Message ID:
Pine.LNX.4.43.0202011045121.29068-100000@chapelperilous.net
On Fri, 1 Feb 2002, Booher Timothy B 1stLt AFRL/MNAC wrote:

> 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.

This can actually be done on the command-line:

perl -pi.bak -e 's/satellite/target/g' *.cpp *.hpp. *.asc

There's nothing wrong with doing it in three steps.

-- Brett
                                          http://www.chapelperilous.net/
------------------------------------------------------------------------
"Trust me":
	Translation of the Latin "caveat emptor."


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