#!/usr/bin/perl -w # # Mass search and replace command line tool # # USAGE: # massreplace "findtext" "replacewith" files... # # To replace with nothing, use NULL # To replace with a carriage return use CR # # MySQL now comes with a bundled "replace" utility that's very good for this too # # Copyright 2005 Matthew Steven http://www.matts.org/ # You may feel free to use it for personal use only under the terms of the GNU GPL # a copy of which can be found at http://gnu.org/copyleft/gpl.html # my($findtext,$replacewith,@FILES)=@ARGV; $findtext =~ s/[\n\r]//g; $findtext =~ s/^('|")//g; $findtext =~ s/('|")$//g; $replacewith =~ s/[\n\r]//g; $replacewith =~ s/^('|")//g; $replacewith =~ s/('|")$//g; unless ($findtext && $replacewith && @FILES > 0){ print "Usage: $0 [Find Text] [Replace Text] Files to change\n";exit; } print "Finding $findtext Replacing with $replacewith Changes\t\tFile"; if($replacewith eq 'NULL'){ $replacewith = ''; } if($findtext eq "CR"){ $findtext = "\r"; } $findtext=quotemeta($findtext); chomp @FILES; foreach my $file (@FILES){ open(F,"<$file") or die $!; my @C=; close(F); my $changes=0; foreach(@C){ $changes += s/$findtext/$replacewith/g;} open(F,">$file") or die $!; print F @C; close(F); print "\n$changes\t\t$file"; } print "\nDone.\n"; exit;