Google

Perl Search and Replace

Perl Search and Replace

Contact:[email protected]

#!/bin/perl
#
# Kevin Kadow ([email protected]) <A&NBSP;HREF="HTTP: ? kadow
www.msg.nethttp://www.msg.net/kadow/
# Can be downloaded from <A&NBSP;HREF="HTTP: www.msg.net search.html? small
utilityhttp://www.msg.net/utility/small/search.html
# For 1stomni, free for all to use as long as the comments are kept intact!
#
# Search and replace strings in html files
#
require 'find.pl';

#
# Change these variables as needed.
#
$dir="/export/home/worldwide/htdocs";

$search="http://www.1888wwsports.com/1stomni.com/virtuflex.cgi";

$replace="http://www.1888wwsports.com/phpshop/cart.php3?action=add";

warn <<"EOF";
Starting search/replace script $0
  Search all files below $dir
  Looking for string $search
  Replace the string with $replace

EOF


#
# Do the actual work
#
&find($dir);
warn "Scanned $files making $changes changes.\n";
exit(0);

################ SUBROUTINES FOLLOW #################################

#wanted
#
# This routine is called by 'find', once for each file, directory, etc.
#
sub wanted {

#Skip everything except files named .html or .shtml
return unless( -f $_ && m/\.[s]*html$/);

&update("$dir/$_");
}


#update
#
# Do the search-and-replace on a given file
#
sub update {
local($file)=@_;

local(@contents);

# Open the file for reading and writing (the + does that)
unless(open(FILE,"+<$file")) {
	warn "Cannot open $file for updating, Error: $!\n";
	return;
	}

warn "Updating $file\n";
$files++;

# Read the contents into an array, replacing the string as we go
while ($_=) {
	$changes += s/$search/$replace/ig;
	push(@contents,$_);
	}

# Go to the beginning of the file
seek(FILE,0,0);

# Update the contents with the data from our array
print FILE (@contents);
close(FILE);
}

###EOF###


Back to the Index