#!/usr/local/bin/perl ####################################################################### # Program: sandr.pl # Description: Search AND Replace expressions in files # Author: Peter Marek (Draconis) ####################################################################### # IF THE USER DOES NOT GIVE AN ARGUMENT THEN DISPLAY USAGE INFORMATION if((!@ARGV) # NO ARGUMENTS || $ARGV[0] ne '-s' # -s NOT PRESENT || !defined $ARGV[1] ) # -s expression NOT PRESENT {&showUsage;} # START RIPPING APART THE COMMAND LINE shift; $searchExpr= shift; if($ARGV[0] eq '-r'){ if(!defined $ARGV[1]){&showUsage;} shift; $replaceExpr=$ARGV[0]; shift; } @files=@ARGV; # NOW THAT WE HAVE ALL THE 'STUFF' WE NEED GO FOR IT # --- SEARCH ONLY if(!defined $replaceExpr){ foreach $fileName (@files){ open(INFILE,"<$fileName"); while(){if(/$searchExpr/){print "$fileName: $_";}} close INFILE; } } # --- SEARCH AND REPLACE else { foreach $fileName (@files){ open(INFILE,"<$fileName"); open(OUTFILE,">.$fileName"); while(){ if(/$searchExpr/){ s/$searchExpr/$replaceExpr/g; print OUTFILE; }else {print OUTFILE;} } close INFILE; close OUTFILE; rename(".$fileName","$fileName"); } } #__________ SUBROUTINES __________ sub showUsage { print <