On Tue, 08 Jun 2004 16:39:18 GMT comments (AT) probertencyclopaedia (DOT) com
(Matt Probert) broke off from drinking a cup of tea at The Probert
Encyclopaedia to write:
Quote:
On the other hand, something similar could be written in a few lines
of Perl and posted here. Okay last to post the (quick and dirty) Perl
solution is a sissy! <g |
Pkay, here's a quick and dirty one. I repeat it's QUICK AND DIRTY and
not a showcase for the most elegant Perl programming ever! Ok? No
flames please!
-- Begin
#!/usr/bin/perl
# A quick and dirty global search and replace program written in Perl
# Supplied as is without warranty
# Usage is 'perl gsr.pl <file spec> <find> <replace>
# There is plenty of scope for improvements, not least of which
# is modifying the program to span line breaks!
# Note:
# A bug in Windows means that file names in lower case, which
# conform to the 8.3 naming convention will be converted to
# uppercase by this program. eg: myfile.htm will be renamed
# MYFILE.HTM
die "Usage is $0 <file spec> <search> <replace>\n" if $#ARGV != 2;
# Extract file names matching ARGV[0] into a list
@fnames=glob($ARGV[0]);
foreach $file (@fnames)
{
open(FILE, $file);
open(TEMP,">temp");
while (<FILE>)
{
# Replace all occurences of the second command line
argument
# with the second within the current line in the file
$_ =~ s/$ARGV[1]/$ARGV[2]/gi;
print TEMP "$_";
}
close(FILE);
close(TEMP);
rename(temp,$file);
}
-- end
--
Free searchable encyclopaedia content for your web site:
http://www.probertencyclopaedia.com/xsearch.htm