Guan boon lee wrote:
> 
> I would like to find a word or character in a file and
> replace it with another word or character, while
> leaving others untouched.  Currently, I am using the
> following method
> 
> #!/bin/perl -Dr -w
> open(FILE,"$ARGV[0]") || die "Can't open $ARGV[0]:
> $!\n";
> open(FILE2,">$ARGV[0].spi") || die "Can't open
> $ARGV[0]_new: $!\n";
> while (<FILE>)
> {
>   if (/(^VV\d+ )(TA)x(\d)x(.*)/)
>   {
>     print FILE2 $1,,$2,"[",$3,"]",$4,"\n";
>   }
>  else
>   {
>     print FILE2 $_;
>   }
> }
> close FILE;
> close FILE2;


Here is one way to do it.

#!/usr/bin/perl -w
use strict;

my $file = shift || die "Usage: $0 filename";

rename $file, "$file.back" or die "Cannot rename $file: $!";
open IN, "$file.back" or die "Cannot open $file.back: $!";
open OUT, "> $file" or die "Cannot open $file: $!";

while ( <IN> ) {
    s/^(VV\d+ TA)x(\d)x/$1[$2]/;
    print OUT;
    }

close OUT;
close IN;

__END__


John
-- 
use Perl;
program
fulfillment

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

Reply via email to