You test only for matching, but of course what you really want is lines after the match. So while you need to make note that it has matched, you need another indication to tell you when to print. And the indication needs to go away once you've printed the limited number of following lines. '$flag' gets set when we match; '$few' allows us to supply the number of lines, or just allow a default value of 2.

use strict; my $flag = 0; my $few = shift || 2; while (<DATA>) { print $_ if ($flag-- > 0); $flag = $few if (/Update certificate/); } __DATA__ Useless line before match Feb 19 15:22:21 206021 152221.684878 7219 INFO: Update certificate f +or user=(XXX) , updated by XXX First line after match Second line after match Third line after match Fourth line after match

Update: output

H:\perl> perl 1156056.pl First line after match Second line after match H:\perl>perl 1156056.pl 3 First line after match Second line after match Third line after match
But God demonstrates His own love toward us, in that while we were yet sinners, Christ died for us. Romans 5:8 (NASB)


In reply to Re: How to print 5 lines below after a regex match by GotToBTru
in thread How to print 5 lines below after a regex match by new2perl2016

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.