perl question - multiple files search and replace

Chris Foote chris at inetd.com.au
Mon Jun 28 02:06:11 CST 2004


On Mon, 28 Jun 2004, Ben Williams wrote:

> I was searching for a method to search and replace a string in files in a 
> tree. I came across a perl-based solution in an email to 
> freebsd-questions at freebsd.org:
>
> -------------------------------
> #!/usr/bin/env perl
>
> use File::Find;
>
> find sub {
>        -f $_ or return;
>        my $file = $_;
>        my $filebak = "${file}~";
>        rename $file, $filebak or die "Can't rename $file to $filebak: $!\n";
>        open FILEBAK, $filebak or die "Can't open $filebak: $!\n";
>        open FILE, ">$file" or die "Can't write to $file: $!\n";
>        while (<FILEBAK>) {
>
>                # Replace OLD with the "search for" -- a regexp
>                # Replace NEW with the "replace with"
>                s/OLD/NEW/g;
>
>                print FILE $_;
>        }
> }, ".";
>
> # end replace.pl
> -------------------------------
>
> Now, I want to just operate on *.php, not on every regular file. I know 
> pretty much zero perl, so can anyone help me out? I'm guessing it needs some 
> kind of:
>        /\.php$/ $_ or return;
> line after the
>        -f $_ or return;
> line, but I'm not sure how to actually write it.

Try:
 	return if ! /\.php$/;


Chris Foote <chris at inetd.com.au>
   _                  _         _    Jabber: chris at jabber.inetd.com.au
  (_)                | |       | |   Director - INETD PTY LTD
   _   _ __     ___  | |_    __| |   Level 2, 132 Franklin St
  | | | '_ \   / _ \ | __|  / _` |   Adelaide SA 5000
  | | | | | | |  __/ | |_  | (_| |   Web:   http://www.inetd.com.au
  |_| |_| |_|  \___|  \__|  \__,_|   Phone: (08) 8410 4566



More information about the linuxsa mailing list