in reply to Re^2: Perl solution for current batch file to extract specific column text
in thread Perl solution for current batch file to extract specific column text
This is the amended (and tested) version:
And this is the output:use strict; use warnings; my $input = "input.txt"; open my $IN, "<", $input or die "cannot open $input $!"; my $output = "output.txt"; open my $OUT, ">", $output or die "cannot open $output $!"; while (my $line = <$IN>) { chomp $line; # remove newline character from end of line if ($line =~ /INTERPOLATED HYDROGRAPH AT (\w+)$/){ print $OUT $1; $line = <$IN> for 1..6; # skip 5 lines my $val2 = (split / /, $line)[1]; # get the second column print $OUT " $val2"; $line = <$IN>for 1..2; # skip one line chomp $line; my $val3 = (split / /, $line)[-1]; # get the last column print $OUT " $val3\n"; } }
I should add that this is not very robust code, it will probably break with any irregularity in the input data. But we would need to know more about the data (having at least three or four samples of line groups, instead of only one) to be able to do something more robust.$ cat output.txt CAC40 1223. 1456.
|
---|