asks for a search and a replace string and parses all files in the current directory or $ARGV[0] with matching filename (the commented regex in the source).. WARNING: always make backups of your data before you use this script. if you enter the wrong values it will trash your data without warning! - useful i.e. for renaming variables in different source files.. - current regex pattern matches .pl and .pm files..
#!/usr/bin/perl $dir = $ARGV[0] || '.'; $!=1; chdir($dir) or die $!; opendir(DIR,$dir) or die $!; my $changes=0; print "\n :: find :. "; $find=<STDIN>; chop($find); print " :: replace :. "; $replace=<STDIN>; chop($replace); $/=undef; foreach my $file (readdir(DIR)) { if($file=~/^.+\.pm$|^.+\.pl$/) # adjust pattern to your needs { print "parsing file $file..\n"; open(FILE,"<$file") or die $!; my $file_data=<FILE>; close(FILE); $changes += $file_data =~ s/$find/$replace/gme; open(FILE,">$file") or die $!; print FILE $file_data; close(FILE); } } print "done. ($changes changes)\n";

Replies are listed 'Best First'.
(jeffa) Re: replace
by jeffa (Bishop) on Jul 20, 2003 at 14:07 UTC