
substituting strings in many files
This procedure replaces all occurrences of 1 string with another in all
the arguments provided on the command line:
perl -pi -e 's/OLDSTRING/NEWSTRING/g' FILELIST
I am trying to write a script "substring.pl" that will do this, but can
be reused. Substring.pl should accept at least 4 arguments:
$OLDSTRING, $NEWSTRING, FILELIST (at least 2 files)
Questions:
1) How can I specify "display USAGE unless at least 4
arguments are given?
2) How can I run the substitution 's///g' counting from the third
argument onwards? I'd like the user to be able to call the script with a
fileLIST.
-------------------------------------------------------------------
Thanks in advance to all who reply. Could you please reply to this message
-------------------------------------------------------------------
The Script I have as of now:-
#!/usr/local/bin/perl
print "*** Substituting string(s) in ", $#ARGV - 1, " files
***\n\n";
}
$OLDSTRING = $ARGV[0];
$NEWSTRING = $ARGV[1];
{
$filename = $ARGV[$i];
s/$OLDSTRING/$NEWSTRING/g
# I need help here ---^
}
sub usage
{
print "\nUSAGE: This script will replace all occurrences of 1 string with
another in all the files given on the command line...\n";
print "EXAMPLE: substring OLDSTRING NEWSTRING FileList... \n\n";
Quote:
}
-------------------------------------------------------------------
Bye,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Mukund Rao ! http://www.*-*-*.com/ ~mrao
Computer Science ! Trinity College * BOX 700416
...............................................................
Receptionist to physician: "The patient is in the middle of a
magazine article and will see you soon, Doctor." --Readers Digest