in reply to Re^2: How to print 5 lines below after a regex match
in thread How to print 5 lines below after a regex match

Set the flag first then print

use strict; my $flag = 0; my $few = shift || 3; while (<DATA>) { $flag = $few if (/Update certificate/); print $_ if ($flag-- > 0); }
poj

Replies are listed 'Best First'.
Re^4: How to print 5 lines below after a regex match
by Laurent_R (Canon) on Feb 24, 2016 at 21:30 UTC
    It really depends on what the OP really needs, but there is a danger of $flag being reset while it is reading the next "few" lines, if the regex matches again. Perhaps something like this:
    use strict; my $few = shift || 3; while (<DATA>) { if (/Update certificate/) { print $_; print scalar <DATA> for 1..$few; } }
    Or possibly for 1..$few-1;, depending on whether $few includes the matching line or not.