Quote:
Originally Posted by ramesh54
Hi Kevin,
I would like to keep the input file as it is. What i am unable to understand is that , in my loop i skip the first 2 lines and then i replace the text in the 3 rd line and print only the third line. But the output I get is for the first line. What is wrong with my loop formation?
Well, if you want to keep the input file as it is and to write modified output to a different file, you need to open another file for writing.
But then, you will not get the required output because of the logical error in your script.
You are not skipping to the third line in your script, but successively incrementing a variable $count and printing $file. The $file will still be referring to line containing *transformation.
You can try this:
-
#!\usr\bin\perl
-
use warnings;
-
use strict;
-
open IN, "z.txt";
-
open OUT,">out.txt";
-
-
while (<IN>){
-
my $file = $_;
-
if ($file =~ /\*transformation/){
-
print OUT $file; #print the line
-
my $dump=<IN>; # skip to next line
-
print OUT $dump;
-
$file=<IN>; # skip to the required line
-
}
-
-
$file =~ s/10 20 30/50 60 80/g;
-
print OUT $file;
-
-
}
-
close IN;
-
close OUT;
-