Category: Utilities
Author/Contact Info Draconis
Description: sandr.pl will Search for an expression in a file or group of files - optionally replacing the Search expression with a Replacement expression. Handy for making global changes to code and such.
#!/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(<INFILE>){if(/$searchExpr/){print "$fileName: $_";}}
        close INFILE;
    }
}

# --- SEARCH AND REPLACE
else {

    foreach $fileName (@files){
        open(INFILE,"<$fileName");
        open(OUTFILE,">.$fileName");
        while(<INFILE>){
            if(/$searchExpr/){
                s/$searchExpr/$replaceExpr/g;
                print OUTFILE;
            }else {print OUTFILE;}
        }
        close INFILE;
        close OUTFILE;
        rename(".$fileName","$fileName");
    }
}

#__________ SUBROUTINES __________

sub showUsage {

    print <<END_OF_HELP;

  Usage:  sandr.pl -s expression [-r expression] filespec

              -s expression = the search expression
              -r expression = the replacement expression
              filespec = the file expression to limit the search

              example:  sandr.pl -s v-msg -r v-message *.p

END_OF_HELP

exit;

}

#__________ POD DOCUMENTATION __________

=head1 NAME

sandr.pl - Search and/or replace expressions in files

=head1 SYNOPSIS

sandr.pl -s searchExp [-r replaceExp] filespec

=item -s searchExp

Use of this switch is mandatory.  It will search for searchExp within 
+files.

=item -r replaceExp

Use of this switch is optional.  It will replace all occurences of Sea
+rchExp 
with replaceExp.

=item filespec

Uses the filespec to derive the files to be searched.  Uses standard U
+NIX 
regualr expression pattern matching.

=head1 DESCRIPTION

sandr.pl will allow for text searches within a set of files and option
+ally 
replace the search text with the replacement text.

=head1 WARNING

Be very careful with this since it does not create a backup of the fil
+e it 
has modified.

=head1 AUTHOR

Peter A. Marek, October 4 2000

=cut
Replies are listed 'Best First'.
Re: Search and Replace (UNIX/Linux)
by LameNerd (Hermit) on Apr 30, 2003 at 18:32 UTC
    You should try using Getopt::Std rather than the roll-your-own
    argument parsing.
    Also did you know *nix systems usaully have a utility called sed
    to do this type of thing. ;)
Re: Search and Replace (UNIX/Linux)
by kral (Monk) on May 05, 2003 at 13:46 UTC
    I suggest the use of Getopt::Std or Getopt::Long and POD::Usage. (You wrote the pod documentation, so why don't use it? :) )

    ------------
    ..::KRaL::..
    (sorry for my english)