use strict; use warnings; use File::ReadBackwards; my $MAX_LENGTH = 1_000_000; my $infile = shift @ARGV; die "'$infile' is not a file!" unless (-f $infile); my $bw = File::ReadBackwards->new( $infile ) or die "can't read '$infile' $!"; my (@block, $length, $line); while( defined( $line = $bw->readline ) ) { unshift @block, $line; $length += length $line; if ($line =~ m/^Title\b/) { print @block; @block = (); $length = 0; } if ($length >= $MAX_LENGTH) { die "$length chars read, without finding a 'Title' line\n". "Input file '$infile' probably has the wrong file format."; } } if (@block) { print STDERR scalar(@block) ." lines not printed out yet!"; }