Re: cut off last lines of a document
- Reply: paul beard : "Re: cut off last lines of a document"
- In reply to: Ede Wolf : "cut off last lines of a document"
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Sun, 03 Sep 2023 04:26:34 UTC
On Fri, Sep 01, 2023 at 10:43:46AM +0200, Ede Wolf wrote:
> From a file/output with an unknown amount of lines, I would like to filter
> out, or not display, the last 3 lines. Is there a way to archive this?
Here's my perl solution (which I have had for a while):
#! /usr/bin/env perl
use strict;
use warnings;
use Getopt::Long qw(:config require_order);
use autodie;
$main::char_mode = 0;
@main::queue = ();
$main::num_dropped = 10;
sub read_one {
my ($next_input) = @_;
our (@queue, $num_dropped);
if ($#queue + 1 >= $num_dropped) {
my $next_output = shift @queue;
print($next_output);
}
push(@queue, $next_input);
}
sub usage {
print STDERR ("usage: notail [-c|--char_mode] [-n|--num_dropped N] [FILE ..]\n");
exit(2);
}
sub main {
our ($char_mode, $num_dropped);
GetOptions
("char_mode" => \$char_mode,
"num_dropped=i" => \$num_dropped)
or usage();
$/ = \1 if $char_mode;
while (<>) {
read_one($_);
}
}
exit(main());
1;
__END__
--
Ian