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

Re: change all files in directory

Thread Previous | Thread Next
Jeff 'japhy' Pinyan
February 1, 2002 07:33
Re: change all files in directory
Message ID:
On Feb 1, Booher Timothy B 1stLt AFRL/MNAC said:

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

There's an entry in the Perl FAQ for preserving case, but there's a much
simpler way:

  sub case {
    my ($s, $d) = @_;
    $s = substr($s, 0, length($d)) if length($d) < length($s);
    $d ^ (~$s & (" " x length $s));

Try it out:

  for (qw( satellite Satellite SATELLITE )) {
    print case($_ => "target"), "\n";

The output is


Its usage in a s/// is:

  s/(word to change)/case($1, "word to become")/eg;

Jeff "japhy" Pinyan
RPI Acacia brother #734
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.

Thread Previous | Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About