http://www.perlmonks.org?node_id=983697


in reply to get n lines before or after a pattern

Untested, but a useful approach.
#!/usr/bin/perl use strict; use warnings; my @buffer; # a queue data structure while ( <DATA> ) { if ( /I sent/ ) { print @buffer; # 3 lines before print; # the matching line print scalar(<DATA>); # 1 line following last; # all done } push @buffer, $_; shift @buffer if @buffer > 3; } __DATA__ this is the output from the command I sent to the command interperter

I'm not really a human, but I play one on earth.
Old Perl Programmer Haiku ................... flash japh