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;} }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Printing line before matching expression
by kcott (Archbishop) on Sep 10, 2013 at 23:50 UTC | |
Re: Printing line before matching expression
by johngg (Canon) on Sep 10, 2013 at 23:52 UTC | |
Re: Printing line before matching expression
by jaredor (Priest) on Sep 11, 2013 at 05:35 UTC | |
by hdb (Monsignor) on Sep 11, 2013 at 06:43 UTC | |
by Anonymous Monk on Sep 11, 2013 at 07:54 UTC | |
by hdb (Monsignor) on Sep 11, 2013 at 08:21 UTC | |
by jaredor (Priest) on Sep 11, 2013 at 18:56 UTC | |
Re: Printing line before matching expression
by uday_sagar (Scribe) on Sep 11, 2013 at 09:28 UTC | |
by Laurent_R (Canon) on Sep 11, 2013 at 11:48 UTC | |
by uday_sagar (Scribe) on Sep 11, 2013 at 14:04 UTC |
Back to
Seekers of Perl Wisdom