use strict;
use warnings;
use Cwd;
use File::Find;
my $search_patternFilePath=$ARGV[0] ;
my $replace_patternFilePath =$ARGV[1];
my $file_pattern = $ARGV[2];
# Usage
(@ARGV == 3 ) || die ("Usage: FindAndReplace.pl pathToFileContaingTheMultiLineSearchText FullPathToFileContainingMultiLineReplaceText FilePattern . Example: perl MultiLineFindAndReplace.pl \"D:\Opera\New Folder\search.txt\" \"D:\Opera\replace.txt\" bak");
find(\&d, cwd);
sub d {
my $file = $File::Find::name;
$file =~ s,/,\\,g;
return unless -f $file;
return unless $file =~ /$file_pattern/;
my $searchPatternString = &slurpFile ( $search_patternFilePath ) ;
my $replacePatternString = &slurpFile ( $replace_patternFilePath ) ;
my $fileStr = &slurpFile ( $file ) ;
$fileStr =~ s/$searchPatternString/$replacePatternString/igo ;
open(FILEHANDLE,">$file") || die "cannot open output file";
print (FILEHANDLE "$fileStr");
close FILEHANDLE ;
}
sub slurpFile
{
my $file = shift ;
print "\$file is $file" ;
local( $/, *FILE ) ;
open (FILE , $file) or
die "Cannot find $file !!! " ;
my $fileString =
close FILE ;
return $fileString ;
}
#Purpose : performs recursive find and replace based on pеrl regexes from the current directory
#the search and replace is case insensitive
#Usage
#perl MultiLineFindAndReplace.pl "D:\Opera\New Folder\search.txt" "D:\Opera\replace.txt" bak
No comments:
Post a Comment
- the first minus - Comments have to be moderated because of the spammers
- the second minus - I am very lazy at moderating comments ... hardly find time ...
- the third minus - Short links are no good for security ...
- The REAL PLUS : Any critic and positive feedback is better than none, so your comments will be published sooner or later !!!!