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; next for 1..5; # skip 5 lines my $val2 = (split / /, $line)[1]; get the second column print $OUT $val2; # a separator may be needed here next; # skip one line my $val3 = (split / /, $line)[-1]; # get the last column print $OUT "$val3\n"; # a separator may be needed here } }