#!/usr/bin/perl use strict; use warnings; my @buffer; # a queue data structure while ( ) { if ( /I sent/ ) { print @buffer; # 3 lines before print; # the matching line print scalar(); # 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