Help for this page
perl -ne 'print @t if /foo/; push @t,$_; shift @t if $#t>5' file
perl -ne 'print @t if /foo/; @t=(@t[1..4],$_)' file