#!/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
In reply to Re: get n lines before or after a pattern
by zentara
in thread get n lines before or after a pattern
by darklord_999
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |