use warnings; use strict; my $exclre = qr/^(?:ls?|man|cat)\b/; my @buf; while (<>) { if (/^#\d+$/ || eof()) { push @buf, $_ if eof(); if (@buf>2 || @buf==2 && $buf[1]!~$exclre) { print @buf; } @buf=(); } push @buf, $_; }