[EMAIL PROTECTED] (Ronald Yacketta) writes:

> This works great with one exception... the two values in the sed need to be
> passed into the script...
> 
> IE:
> changedbsid.pl OLDSID NEWSID filename(s)
> 
> could be ran against 1 or more files at a time.
> 
> I havethis which works like a champ on a SINGLE file
> 
> #!/usr/bin/perl -w
> 
> $^I = ".bak";
> 
> if ( @ARGV != 3 ) {
>         print "You must supply two command line options\n";
>         print "The OLD TOEKN and the NEW TOEKN\n";
>         print "\n IE: $0 _VALUTEST _QAP2\n";;
>         exit (1);
> }
> 
> $OLD = $ARGV[0];
> $NEW = $ARGV[1];
> $FILES = $ARGV[2];
> @ARGV = glob $FILES;
> while(<>) {
>         s/\Q$OLD\E/$NEW/g;
>         print;
> }
> 
> my problem is with multiple files, I could axe the ARGV check above.. but
> that wont get me anywhere I do not think...

The changes I made:

1. Lower case variable names.  More Perl-ish, and less eye
   and shift-key strain.

2. Move the glob from Perl to the command line.  That makes
   @ARGV pre-populated with the filenames.  (This may not
   work on WinDOS systems.  I don't know; I'm used to having
   glob done by the Korn shell in Unix.)  

2a.  The program now works on STDIN by specifying no
     filename arguments.  (It may work by specifying "-" as
     an argument; I forget).

2b.  The program now works for multple file specifications.
     It isn't limited to working with one file glob spec.
     Specify 'em all on the line, it'll process 'em all.

3. Conditional setting of in-place editing argument.  I'd
   bet that it worked OK to set it for STDIN, but I don't
   have a Perl installation on my traveling laptop to check,
   so I played it safe by undef'ing it for STDIN
   processing. 

4. Shift off the required leading arguments rather than
   accessing them directly through indices.  This leaves
   @ARGV to contain only the filenames.
        
The changes I didn't (really) make:

1. My check for the number of args is basically the same as
   yours.  I mucked with it for my own personal style
   reasons since I was alrady playing with ARGV for
   globbing.

Here's how I'd do it.

================================================================
#!/usr/bin/perl -w

$program_name = $0;

# Print a usage message to STDERR
sub usage {
    print STDERR "USAGE: $program_name old_token new_token [files...]\n";
}

unless ( @ARGV >= 2 ) {
    usage;
    exit 1;
}

$old_token = shift @ARGV;
$new_token = shift @ARGV;

# Elements left in @ARGV are filenames.
# If @ARGV is empty, use STDIN.
# Make backups if there are filenames to process.
$^I = @ARGV ? ".bak" : undef;

while(<>) {
    s/\Q$old_token\E/$new_token/g;
    print;
}

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to