#!/usr/bin/env perl
use warnings;
use strict;
my $timestamp;
my $entryline;
my $timestamp_regex = qr/^#([[:digit:]]{10})$/;
my $exclusion_regex = qr/^(?:ls?|man|cat)\b/;
my $state = "begin";
while (<>) {
if ($state eq "begin") {
if (/$timestamp_regex/) {
$timestamp = $_;
$state = "readtimestamp";
}
else {
print;
$state = "printedline";
}
}
elsif ($state eq "printedline") {
if (/$timestamp_regex/) {
$timestamp = $_;
$state = "readtimestamp";
}
else {
print;
$state = "printedline";
}
}
elsif ($state eq "readtimestamp") {
if (/$timestamp_regex/ && $1 >= $timestamp) {
$timestamp = $_;
$state = "readtimestamp";
}
elsif (/$exclusion_regex/) {
$entryline = $_;
$state = "readentryline";
}
else {
print $timestamp;
print;
$state = "printedline";
}
}
elsif ($state eq "readentryline") {
if (/$timestamp_regex/) {
$entryline = "";
$timestamp = $_;
$state = "readtimestamp";
}
else {
print $timestamp;
print $entryline;
print;
$state = "printedline";
}
}
}
####
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, $_;
}
####
use warnings;
use strict;
my $exclre = qr/(?:ls?|man|cat)\b/;
$/ = "\n#";
while (<>) {
s/\n#?\z//;
print /\A#/?():'#', $_, "\n"
unless /\A#?\d+\n$exclre(?!.*\n)/;
}