Date: Sun, 3 Sep 2023 14:16:54 -0700 From: paul beard <paulbeard@gmail.com> To: questions@freebsd.org Subject: Re: cut off last lines of a document Message-ID: <CAMtcK2qbc1jQHXJ%2Bwu0F62o6aDQKYAx3kYARuQFZLHS6DJktRg@mail.gmail.com> In-Reply-To: <uy2fdgobbejnhyctyyrbhgvhutuhtbzzxwg5wukdujyhw7h25u@74ujfxm5gjlk> References: <57be5495-97f8-4f22-9ae2-cd9712596e64@nebelschwaden.de> <uy2fdgobbejnhyctyyrbhgvhutuhtbzzxwg5wukdujyhw7h25u@74ujfxm5gjlk>
next in thread | previous in thread | raw e-mail | index | archive | help
[-- Attachment #1 --]
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/
[-- Attachment #2 --]
<div dir="ltr">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. <div><br></div><div>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<br>export WANT=`echo "$COUNT-3" | bc ` # subtract 3 (or however many)<br>head -$WANT /var/log/messages # display the remainder. <br></div><div><br></div><div>might seem inefficient but when a raspberry π is clocked in GHz, who cares? </div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Sep 2, 2023 at 9:27 PM <<a href="mailto:little.analyst892@aceecat.org">little.analyst892@aceecat.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(204,204,204);padding-left:1ex">On Fri, Sep 01, 2023 at 10:43:46AM +0200, Ede Wolf wrote:<br>
<br>
> From a file/output with an unknown amount of lines, I would like to filter<br>
> out, or not display, the last 3 lines. Is there a way to archive this?<br>
<br>
Here's my perl solution (which I have had for a while):<br>
<br>
#! /usr/bin/env perl<br>
<br>
use strict;<br>
use warnings;<br>
use Getopt::Long qw(:config require_order);<br>
use autodie;<br>
<br>
$main::char_mode = 0;<br>
@main::queue = ();<br>
$main::num_dropped = 10;<br>
<br>
sub read_one {<br>
my ($next_input) = @_;<br>
our (@queue, $num_dropped);<br>
if ($#queue + 1 >= $num_dropped) {<br>
my $next_output = shift @queue;<br>
print($next_output);<br>
}<br>
push(@queue, $next_input);<br>
}<br>
<br>
sub usage {<br>
print STDERR ("usage: notail [-c|--char_mode] [-n|--num_dropped N] [FILE ..]\n");<br>
exit(2);<br>
}<br>
<br>
sub main {<br>
our ($char_mode, $num_dropped);<br>
GetOptions<br>
("char_mode" => \$char_mode,<br>
"num_dropped=i" => \$num_dropped)<br>
or usage();<br>
$/ = \1 if $char_mode;<br>
while (<>) {<br>
read_one($_);<br>
}<br>
}<br>
<br>
exit(main());<br>
<br>
1;<br>
__END__<br>
<br>
<br>
<br>
-- <br>
Ian<br>
<br>
</blockquote></div><br clear="all"><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature">Paul Beard / <a href="http://www.paulbeard.org/" target="_blank">www.paulbeard.org/</a><br></div>
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAMtcK2qbc1jQHXJ%2Bwu0F62o6aDQKYAx3kYARuQFZLHS6DJktRg>
