Randy Johnson
asked on
Search / Replace Script, Little Update
I would like a slight alteration of this script
I would like to be able to do this:
swap.pl swap.cfg c:/reports
and have it go through c:/reports and do in place search / replace in the files
Here is a sample swap.cfg search for Replacewith
.report.html .report.pl
or if someone could explain how the config file works below that would be cool too.
#!/usr/bin/perl
#SWAP.PL by J.Tom Germain, tg@cgiware.org
#September, 1996
#Copyright 1996-2000
#You are free to use & modify this script for your own purposes as long
#as it includes the credit you see on line 3, and don't sell it.
#I am not responsible for losses or injury of any kind resulting from the
#use of this script
#Other freeware available at http://www.cgiware.org
#####GET ARGUMENTS
if($#ARGV < 0){
print "Enter string to search for:\n ";
$search = <STDIN>;
print "\nEnter string to replace it with:\n ";
$replace = <STDIN>;
print "\nEnter directory path to search:\n ";
$dirpath = <STDIN>;
$doit[0] = "$search^^$replace^^$dirpa th";
}
else{
$filename = $ARGV[0];
if($filename !~ /\w/){
print "You must specify a configuration file.\n";
print "If you wish to enter parameters manually, don't put arguments.\n";
exit(1);}
$dirpath = $ARGV[1];
if($filename !~ /\./){$filename .= '.rep';}
$filename =~ s/\s//g;
open(FIL, "<$filename") || die "Can't open $filename\n";
@doit = <FIL>;
close(FIL);
}
print "\nStarting search and replace...(Copyright 1996-2000 Tom Germain)\n";
$count = 0;
loopz:
foreach $line (@doit){
$line =~ s/(\n|\r|\t)//g;
($search,$replace,$newdirp ath) = split(/\^\^/,$line);
if($newdirpath ne ""){$dirpath = $newdirpath;}
if($dirpath eq ""){$dirpath = '.';}
$dirpath =~ s/\s//g;
if($dirpath !~ /(\/)$/){$dirpath .= '/';}
print "Opening $dirpath\n";
opendir(DIR, "$dirpath") || next;
print "Okay\n";
@files = readdir(DIR);
closedir(DIR);
@files = grep(!/^(\.|\.\.)$/,@files );
loopy:
foreach $file (@files){
if(!(-f "$dirpath$file")){
if(-d "$dirpath$file"){
$doit[$#doit + 1] = "$search^^$replace^^$dirpa th$file";
}
next;
}
open(FIL,"<$dirpath$file") || next;
@data = ();
@data = <FIL>;
close(FIL);
loopx:
if($num = grep(/$search/i,@data)){
foreach $line (@data){
if($line =~ s/$search/$replace/gi){$co unt++;}
}
open(FIL,">$dirpath$file") || next;
seek(FIL,0,0);
print FIL @data;
close(FIL);
} #end loopx
} #end loopy
} #end loopz
print "\nDONE! Total matches replaced: $count\n";
I would like to be able to do this:
swap.pl swap.cfg c:/reports
and have it go through c:/reports and do in place search / replace in the files
Here is a sample swap.cfg search for Replacewith
.report.html .report.pl
or if someone could explain how the config file works below that would be cool too.
#!/usr/bin/perl
#SWAP.PL by J.Tom Germain, tg@cgiware.org
#September, 1996
#Copyright 1996-2000
#You are free to use & modify this script for your own purposes as long
#as it includes the credit you see on line 3, and don't sell it.
#I am not responsible for losses or injury of any kind resulting from the
#use of this script
#Other freeware available at http://www.cgiware.org
#####GET ARGUMENTS
if($#ARGV < 0){
print "Enter string to search for:\n ";
$search = <STDIN>;
print "\nEnter string to replace it with:\n ";
$replace = <STDIN>;
print "\nEnter directory path to search:\n ";
$dirpath = <STDIN>;
$doit[0] = "$search^^$replace^^$dirpa
}
else{
$filename = $ARGV[0];
if($filename !~ /\w/){
print "You must specify a configuration file.\n";
print "If you wish to enter parameters manually, don't put arguments.\n";
exit(1);}
$dirpath = $ARGV[1];
if($filename !~ /\./){$filename .= '.rep';}
$filename =~ s/\s//g;
open(FIL, "<$filename") || die "Can't open $filename\n";
@doit = <FIL>;
close(FIL);
}
print "\nStarting search and replace...(Copyright 1996-2000 Tom Germain)\n";
$count = 0;
loopz:
foreach $line (@doit){
$line =~ s/(\n|\r|\t)//g;
($search,$replace,$newdirp
if($newdirpath ne ""){$dirpath = $newdirpath;}
if($dirpath eq ""){$dirpath = '.';}
$dirpath =~ s/\s//g;
if($dirpath !~ /(\/)$/){$dirpath .= '/';}
print "Opening $dirpath\n";
opendir(DIR, "$dirpath") || next;
print "Okay\n";
@files = readdir(DIR);
closedir(DIR);
@files = grep(!/^(\.|\.\.)$/,@files
loopy:
foreach $file (@files){
if(!(-f "$dirpath$file")){
if(-d "$dirpath$file"){
$doit[$#doit + 1] = "$search^^$replace^^$dirpa
}
next;
}
open(FIL,"<$dirpath$file")
@data = ();
@data = <FIL>;
close(FIL);
loopx:
if($num = grep(/$search/i,@data)){
foreach $line (@data){
if($line =~ s/$search/$replace/gi){$co
}
open(FIL,">$dirpath$file")
seek(FIL,0,0);
print FIL @data;
close(FIL);
} #end loopx
} #end loopy
} #end loopz
print "\nDONE! Total matches replaced: $count\n";
ASKER
I found the documentation go wayback http://web.archive.org/web/19990420222443/http://cgiware.com/swap.htm
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
push @replace,[split];
$_ = qr/\Q$_\E/ for $replace[-1][0];
last if eof;
}
{local $^I=".bak";
while( $line=<> ){
$line =~s/$_->[0]/$_->[1]/g for @replace;
print $line;
}
}