Re: cut off last lines of a document
- Reply: Ede Wolf : "Re: cut off last lines of a document"
- In reply to: little.analyst892_a_aceecat.org: "Re: cut off last lines of a document"
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Sun, 03 Sep 2023 21:16:54 UTC
This seems to meet the requirements of displaying a file minus the last
three lines. It seems portable as well: most unix-ish systems should have
these utilities. I tried to avoid any regexes…as the saying goes, adding a
regex means now you have *two* problems. At least for me it seems to go
that way.
export COUNT=`wc -l /var/log/messages | tr -d -c '\n[:digit:]'` # get the
number of lines in the file: if needed/the filename has digits use 'cut -f1
-d "/" to isolate
export WANT=`echo "$COUNT-3" | bc ` # subtract 3 (or however many)
head -$WANT /var/log/messages # display the remainder.
might seem inefficient but when a raspberry π is clocked in GHz, who cares?
On Sat, Sep 2, 2023 at 9:27 PM <little.analyst892@aceecat.org> wrote:
> 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
>
>
--
Paul Beard / www.paulbeard.org/