On Jul 24, 2004, at 11:38 AM, <[EMAIL PROTECTED]> wrote:

I am trying to write a search and replace script that can accept multiple
arguments, but i want the first argument to be the filename to read, the
next one to be the string to search for, the next one to be the replacement
string, and the last one to be the name of the new file it creates with the
new changes, but i can't figure out how to seperate each argument, can some
one tell me how to do something like this. Any help is very much
appreciated.

Well, command line arguments come into the program by way of the array @ARGV. So first we should be sure you got the right number of arguments:


die "Usage:  script OLD_FILE, SEARCH, REPLACE, NEW_FILE\n"
                unless @ARGV == 4;

Then we can use it:

my($old_file, $search, $replace, $new_file) = @ARGV;

Finally, just FYI, you can do what you describe with a one-liner:

perl -pi.bak -e 's/search/replace/g' old_file

Hope that helps.

James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to