Another question, so far i have this script:

#!/usr/bin/perl

use IO::File;

die "Usage:  script OLD_FILE, SEARCH, REPLACE, NEW_FILE\n"
                unless @ARGV == 4;
my($old_file, $search, $replace, $new_file) = @ARGV;

 $fh = new IO::File "> $new_file";
     if (defined $fh) {
        while(<>){
        s/$search/$replace/g;
        print;
        }
        $fh->close;
    }


But the first problem is that if i try to run the code like this:

perl script.pl old.txt , \n new.txt

it will remove all the commas but won't replace them with new line
characters it will just remove them.

Also when i try to run the script it gives me this error:

Can't open ,: No such file or directory at script.pl line 11, <> line 1.
Can't open \n: No such file or directory at script.pl line 11, <> line 1.
Can't open new.txt: No such file or directory at script.pl line 11, <> line
1.


Why am i getting these errors, and how can i fix this?


Thanks in advance.


-----Original Message-----
From: James Edward Gray II [mailto:[EMAIL PROTECTED]
Sent: Saturday, July 24, 2004 3:13 PM
To: [EMAIL PROTECTED]
Cc: [EMAIL PROTECTED]
Subject: Re: Multiple Parameters


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>




-- 
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