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

Re: change all files in directory

Thread Previous | Thread Next
From:
Jeff 'japhy' Pinyan
Date:
February 1, 2002 07:33
Subject:
Re: change all files in directory
Message ID:
Pine.GSO.4.21.0202011030140.27903-100000@crusoe.crusoe.net
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

  target
  Target
  TARGET

Its usage in a s/// is:

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

-- 
Jeff "japhy" Pinyan      japhy@pobox.com      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** 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


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