Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 2 Sep 2023 21:26:34 -0700
From:      little.analyst892@aceecat.org
To:        questions@freebsd.org
Subject:   Re: cut off last lines of a document
Message-ID:  <uy2fdgobbejnhyctyyrbhgvhutuhtbzzxwg5wukdujyhw7h25u@74ujfxm5gjlk>
In-Reply-To: <57be5495-97f8-4f22-9ae2-cd9712596e64@nebelschwaden.de>
References:  <57be5495-97f8-4f22-9ae2-cd9712596e64@nebelschwaden.de>

next in thread | previous in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?uy2fdgobbejnhyctyyrbhgvhutuhtbzzxwg5wukdujyhw7h25u>