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
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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |