http://www.perlmonks.org?node_id=125855

I maintain a badly designed website and have to often make a single change to over 400 static webpages. Perl to the rescue! I cobbled this together so it doesnt' have a lot of fancy error-checking but it does open every file of every subdirectory (without recursion) and replaces every instance of your search string with your replace string which it asks on the tty. It then backs up the file it read (by adding .byProc to the end of the filename) and saves the new file. Not the best but it certainly works. You call it by ./progName /directory/path -clean. The -clean switch will search the directories for the .byProc files and unlink them. No safety net!
#!/usr/bin/perl -w my @dirList; my @fileList; my $totalEdits = 0; $procDir = $ARGV[0]; if (!$ARGV[0]) { print "Give directory: "; chomp($procDir = <STDIN>); } if ($ARGV[1]) { if ($ARGV[1] eq "-clean") { $cleanUp = 1; $unlinkedFiles = 0; } } push @dirList, $procDir; print "Processing."; while (!$toDie) { $thisDir = shift(@dirList); if ($thisDir) { opendir(CURRDIR, $thisDir); @tempDirList = readdir(CURRDIR); rewinddir(CURRDIR); close(CURRDIR); print "."; foreach ($i = 0; $i < ($#tempDirList + 1); $i++) { if (substr($tempDirList[$i],0,1) ne ".") { $completeHandle = $thisDir . "/" . $tempDirList[$i]; if (-d $completeHandle) { push @dirList, $completeHandle; } elsif (-T $completeHandle) { push @fileList, $completeHandle; if ($cleanUp && index($completeHandle, ".byProc", +0) > 0) { unlink $completeHandle or print "Error unlinki +ng $completeHandle: $!\n"; $unlinkedFiles++; } } } } } else { $toDie = 1; if ($unlinkedFiles) { $unlinkedFiles--; if ($unlinkedFiles == 0) { $unlinkedFiles = "No"; } } print "\n"; } } if ($unlinkedFiles) { print "\n\n$unlinkedFiles files deleted.\n\n"; } if (!$cleanUp) { print "\n\n" . $#fileList . " files found.\n\n"; while (!$canGo) { print "Process all files found? <y|n>\n"; chomp($goOn = <STDIN>); if ($goOn eq "y") { #work with each file $canGo = 1; } elsif ($goOn eq "n") { print "Exiting...\n"; exit; } else { print "Must be either yes [y] or no [n]\n"; } } print "Please enter search criteria (can be RegExp):\n"; chomp($toFind = <STDIN>); print "Please enter what you would like to replace it with:\n" +; chomp($toReplace = <STDIN>); foreach ($i = 0; $i < ($#fileList + 1); $i++) { open(FH, $fileList[$i]); $fileContents = <FH>; close(FH); my @fileParts = split($toFind,$fileContents); $totalEdits += ($#fileParts--); if ($#fileParts < 1) { #print "No instances of \"$toFind\" found in $fileList +[$i].\n"; } else { $oldFile = $fileList[$i] . ".byProc"; open(FH, ">>$oldFile"); print FH $fileContents; close(FH); open(FH,">$fileList[$i]"); $fileContents =~ s/$toFind/$toReplace/eg; print FH $fileContents; close(FH); print "Replaced \"$toFind\" with \"$toReplace\" $#file +Parts times in $fileList[$i]\n"; } } print "$totalEdits made in $#fileList files\n"; }
Feedback appreciated!

CiceroLove
Fates! We will know your pleasures: That we shall die, we know; 'Tis but the time, and drawing days out, that men stand upon. - Act III,I, Julius Caesar