
Substituting Strings in Many Files
Quote:
> file as "subst.pl". I would like to invoke this Perl script in the future
> by calling it at the bash$ prompt:
> subst OLDSTRING NEWSTRING FILE1 FILE2 FILE3 FILEn...
Try calling it subst, and using chmod u+x subst
Quote:
> perl -pi.bak -e 's/OLDSTRING/NEWSTRING/g' FILELIST...
#!/usr/bin/perl
open(FILE, $file);
foreach $line (<FILE>) {
$line =~ s/$old/$new/g;
print $line;
}
close(FILE);
Quote:
}
That doesn't do everything, like split-line strings, and it
directs everything to STDOUT. But to fix that, use this:-
#!/usr/bin/perl
open(FILE, $file);
open(TMP, "tmp");
foreach $line (<FILE>) {
$line =~ s/$old/$new/g;
print TMP $line;
}
close(TMP);
close(FILE);
system("cp -i tmp $file");
Quote:
}
Ok, so eventually you'll want to get rid of the -i, but until
you're 100% sure this is safe, you might want to leave it in. And who
knows, you may like it :).
--
---------------------------------------------------------------------
| Name: Tim Nelson | Live and learn, die and forget -- |
---------------------------------------------------------------------
Web: http://www.deakin.edu.au/~tsn/
----BEGIN GEEK CODE BLOCK----
Version 3.1
GCS d? s: a--->-- C++>++++$ US+ P+ L>++ E- W+++ N+ w+> M-- V- Y+>++ PGP->++ R(+)
!tv B++ DI++++ D+ G e>++ h!/* y-
-----END GEEK CODE BLOCK-----