http://qs321.pair.com?node_id=1053380

rm has asked for the wisdom of the Perl Monks concerning the following question:

I want to print the nth line (typically 2nd) before the line which has matching expression. I have code below to print the nth line after matching expression. I'd like to see if we can keep the same structure. My first time here so pls be patient if I made elementary mistakes. thx!

open (INPUT, $ARGV[0]) or die "I couldn't get at input text"; open (OUTPUT, '>outfile.txt') or die "Can't write to outfile: $!"; while ($line = <INPUT>) { #chop ($line); if (( $line =~ /index_1/ ) and ($line =~ /6/)) { $i=0; while ($i<2) { $line = <INPUT>; # gets next line $i++; } print OUTPUT $line;} }