#! /usr/local/bin/perl use Getopt::Long; { # Check command-line arguments for options GetOptions("h", "help"); # check if a datafile was supplied $narg = @ARGV; if ($narg < 3) { print "no... wrong number of arguments: $narg\n\n"; print_usage(); exit; } # check if the user wants help if (($opt_h) || ($opt_help) ) { print_usage(); exit; } # this is the string the program will find... $str1 = $ARGV[0]; # this is the replacement it will use... $str2 = $ARGV[1]; # ... and here's where it looks. @file_list = @ARGV[2..$narg-1]; for ( @file_list) { $datafile = $_; $newdf = $datafile."~"; rename($datafile, $newdf) || die "Could not rename '$datafile'"; open(DATAFILE, "<$newdf") || die "Could not open datafile '$newdf'"; open(FIXDFILE, ">$datafile") || die "Could not open datafile '$datafile'"; # This is the main loop of the program. # It reads every line in and sticks the data into a data structure. $counter = 0; $subcount= 0; while ($dataline = ) { #remove newline from dataline #chop ($dataline); #print "$dataline\n
"; # do the fix... /o = compile search string once (it's a constant!) # ... /g = global (multiple replaces ok in a single line) $subcount += ($dataline =~ s!$str1!$str2!go); $counter++; (print FIXDFILE $dataline) || die "Could not write to '$datafile'"; } print "searchandreplace: [$datafile] $subcount replacements made on $counter lines.\n"; close(DATAFILE) || die "Could not close datafile '$newdf'"; close(FIXDFILE) || die "Could not close datafile '$datafile'"; } print "\nDon't forget to chmod a+r webfiles !!\n"; } sub print_usage { print STDOUT <