[wplug] replacing text within full website

James O'Kane jo2y at midnightlinux.com
Tue Jan 6 09:23:52 EST 2004


On Tue, 6 Jan 2004, Joe wrote:
> #!/usr/bin/perl
> 
> open (FILE, "$ARGV[0]");
> open (FILE2, ">$ARGV[0].tmp");
> while (<FILE>) {
> 	$_ =~ s/kingpin/www.foo.bar/g;
> 	print FILE2 $_;
> }
> close(FILE);
> close(FILE2);
> rename("$ARGV[0].tmp", "$ARGV[0]");

This isn't a lynching, just a passing of wizdom.

that is fairly close to:

perl -pi.tmp -e 's/kingpin/www.foo.bar/g;' *

If I remember correctly, the -p adds the loop, the -i makes a backup of 
the original file with a .tmp extention and the -e executes that line. I 
remember then as -pie.
It's taken months for me to think of that instinctively when I have a 
quick search and replace problem. Before I was doing weird contortions 
with bash and sed. Such as:
for x in *
do
 mv $x $x.tmp
 cat $x.tmp | sed 's/search/replace/g' > $x
done


-james





More information about the wplug mailing list