in reply to Re^3: How can I print three lines before pattern match?
in thread How can I print three lines before pattern match?

Thank you very much for your help.

I'm sorry to bother, but how can I add the existing matched line with the three lines?

++, but that should be my @history = ('') x 3;
++, but that should be my @history = ('') x 3 + $.; #???

Thank you agian.

Replies are listed 'Best First'.
Re^5: How can I print three lines before pattern match?
by ikegami (Patriarch) on Aug 17, 2009 at 02:23 UTC
Re^5: How can I print three lines before pattern match?
by neurotoxx (Novice) on Aug 17, 2009 at 16:43 UTC

    Figured it out...to add the line that contains the match string, I added an additional line to the loop. Probably not the best way, but it works.

    while (<$_>) { print OUT @history if /$pattern/; print OUT $_ if /$pattern/; # adds line with match push @history, $_; shift @history; }

    Thanks again every one,

    Neurotoxx

      Probably not the best way

      Indeed. You perform the regex match twice for nothing. The two previously submitted solutions are better. -- for asking for help then ignoring the answers twice.