search & replace on multiple files

oremanj at get-linux.org oremanj at get-linux.org
Wed Jul 23 13:21:25 PDT 2003


On Wed, Jul 23, 2003 at 01:20:40PM -0700 or thereabouts, oremanj at get-linux.org wrote:
> On Wed, Jul 23, 2003 at 10:56:19AM -0400 or thereabouts, Scott I. Remick wrote:
> > Hello... I'm trying to figure out a way to pull off the following:
> > 
> > I have a subdir with many different sorts of text files (some nested in
> > additional subdirs, so recursion would be necessary) that need to have a
> > search & replace done on them. What's a quick way to script a global search
> > & replace on many/all text files in nested subirs? 
> 
> Hard way (tested):
> 
> #!/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
> 
> Easy way (not tested):
> 
> find . -type f |   perl -pi~ -e 's/OLD/NEW/g;'
                   ^ OOPS, sorry, there should be an `xargs' here

-- Josh

> 
> -- Josh
> 
> > 
> > Thanks!
> > 
> > _______________________________________________
> > freebsd-questions at freebsd.org mailing list
> > http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> > To unsubscribe, send any mail to "freebsd-questions-unsubscribe at freebsd.org"
> _______________________________________________
> freebsd-questions at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe at freebsd.org"


More information about the freebsd-questions mailing list