Skip to content

Instantly share code, notes, and snippets.

@ryanschuhler
Created March 6, 2013 00:56
Show Gist options
  • Save ryanschuhler/5095848 to your computer and use it in GitHub Desktop.
Save ryanschuhler/5095848 to your computer and use it in GitHub Desktop.
Here is a script I wrote to find and replace things on linux. You might be able to modify it for windows if you install perl, but Im not sure.
#!/usr/bin/perl
print "Find\n"; ##this defines what you are searching for, it will be inserted into a regex expression, so escape character if needed
$test1 = <STDIN>;
chomp $test1;
print "Replace\n"; ##this defines what you will replace the previous string with, also in regex so escape special characters
$test2 = <STDIN>;
chomp $test2;
print "Where\n"; ##where you want it to look. ie. ~/Desktop/test (this will search all files in the directory and its subdirectories)
$test3 = <STDIN>;
chomp $test3;
print "Type\n"; ##this determins the type of file you want to search. ie. xml (leave this blank and it will search all files)
$test4 = <STDIN>;
chomp $test4;
if ($test4) {
$test5 = " -name *.$test4)";
} else {
$test5 = ")";
}
print "Find $test1 and replace with $test2 in $test3\n";
system "perl -e \"s/$test1/$test2/g;\" -pi \$(find $test3 -type f$test5";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment