Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 161
  • Last Modified:

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^^$dirpath";
}
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,$newdirpath) = 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^^$dirpath$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){$count++;}
}

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";

0
rjohnsonjr
Asked:
rjohnsonjr
1 Solution
 
ozoCommented:
while( <> ){
   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;
}
}
0
 
rjohnsonjrAuthor Commented:
0
 
GranModCommented:
Closed, 500 points refunded.
GranMod
The Experts Exchange
Community Support Moderator of all Ages
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now