#!/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

In reply to Search and Replace (UNIX/Linux) by draconis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.