Skip to content

Instantly share code, notes, and snippets.

@Jexulie
Created June 14, 2019 05:47
Show Gist options
  • Save Jexulie/0d71301a1aa320ccecd7aa14b03cf998 to your computer and use it in GitHub Desktop.
Save Jexulie/0d71301a1aa320ccecd7aa14b03cf998 to your computer and use it in GitHub Desktop.
Perl script for recursive String swapping in files.
use strict;
use Getopt::Std;
my $count = 0;
#? Recursive Swap Function
# @param1 = String
# @param2 = Searched
# @param3 = Replace
sub Swap {
my ($string, $searched, $replace) = @_;
my $newString = "";
my $re = qr/$searched/;
# print "Before: $`\n";
# print "Matched: $&\n";
# print "After: $'\n";
if($string =~ $re){
$count += 1;
return Swap($newString.$`.$replace.$', $searched, $replace);
}else{
return $string;
}
}
sub TextExtractor {
my $fileName = shift;
open my $fh, '<', $fileName or die $!;
my $content = do {local $/; <$fh>};
return $content;
}
sub EditFile {
my $fileName = shift;
my $content = shift;
open(FH, '>', $fileName) or die return 0;
print FH $content;
close(FH);
return 1;
}
sub Walker {
my ($dir, $ext, $ser, $rep) = @_;
opendir(FOLDER, $dir) or die $!;
chdir($dir) or die $!;
my $patt = qr/^.*\.$ext/;
while(my $file = readdir FOLDER){
if($file ne "." && $file ne ".."){
my $fullPath = $dir.'/'.$file;
if(-d $file){
print "Found Folder: $dir/$file\n";
Walker($fullPath, $ext, $ser, $rep);
}else{
if($file =~ $patt){
print "Found File: $dir/$file\n";
my $content = TextExtractor($fullPath);
my $changedContent = Swap($content, $ser, $rep);
my $res = EditFile($fullPath, $changedContent);
if($res){
print "$fullPath contents has been changed !\n";
}else{
print "While Changing $fullPath content there was an error ..\n";
}
}
}
}
}
}
sub CheckPath {
my $path = shift;
if($path =~ /^.*\..*$/){
return 1; # is File
}else{
return 0; # is Folder
}
}
sub Main {
my ($descision, $path, $ext, $searched, $replace) = @_;
if($path && $searched && $replace){
if($descision){
if(CheckPath($path)){
my $content = TextExtractor($path);
my $changedContent = Swap($content, $searched, $replace);
my $res = EditFile($path, $changedContent);
if($res){
print "Successfully changed File contents\n";
return 1;
}else{
print "Error at changing File !\n";
return 0;
}
}else{
print "Invalid Path\n";
}
}else{
if(CheckPath($path)){
print "Invalid Path\n";
return 0;
}else{
if($ext){
Walker($path, $ext, $searched, $replace);
print "$count, file(s) Changed!\n";
return 1;
}else{
print "Extension Argument is Empty\n";
}
}
}
}else{
print "Invalid or Empty Arguments\n";
return 0;
}
}
my %options = ();
getopts("hdp:e:s:r:", \%options);
# d parameter changes only one file
# if its false recursively changes every file with extension
# print "$options{d}\n" if defined $options{d};
# print "$options{p}\n" if defined $options{d};
# print "$options{e}\n" if defined $options{d};
# print "$options{s}\n" if defined $options{d};
# print "$options{r}\n" if defined $options{d};
if($options{h}){
print "Word Swapper - Parameters;
-d [Optional] Search for single File.
-p Path.
-e [Optional] Extension.
-s Searched String.
-r Replaced String.\n"
}else{
Main($options{d}, $options{p}, $options{e}, $options{s}, $options{r});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment