Skip site navigation (1)Skip section navigation (2)
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
--00000000000079f4cc06047aea6e
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

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=E2=80=A6as the saying goes, a=
dding a
regex means now you have *two* problems. At least for me it seems to go
that way.

export COUNT=3D`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=3D`echo "$COUNT-3" | bc ` # subtract 3 (or however many)
head -$WANT /var/log/messages # display the remainder.

might seem inefficient but when a raspberry =CF=80 is clocked in GHz, who c=
ares?

On Sat, Sep 2, 2023 at 9:27=E2=80=AFPM <little.analyst892@aceecat.org> wrot=
e:

> 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 =3D 0;
> @main::queue =3D ();
> $main::num_dropped =3D 10;
>
> sub read_one {
>     my ($next_input) =3D @_;
>     our (@queue, $num_dropped);
>     if ($#queue + 1 >=3D $num_dropped) {
>         my $next_output =3D shift @queue;
>         print($next_output);
>     }
>     push(@queue, $next_input);
> }
>
> sub usage {
>   print STDERR ("usage: notail [-c|--char_mode] [-n|--num_dropped N] [FIL=
E
> ..]\n");
>   exit(2);
> }
>
> sub main {
>     our ($char_mode, $num_dropped);
>     GetOptions
>         ("char_mode" =3D> \$char_mode,
>          "num_dropped=3Di" =3D> \$num_dropped)
>         or usage();
>     $/ =3D \1 if $char_mode;
>     while (<>) {
>         read_one($_);
>     }
> }
>
> exit(main());
>
> 1;
> __END__
>
>
>
> --
> Ian
>
>

--=20
Paul Beard / www.paulbeard.org/

--00000000000079f4cc06047aea6e
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">This seems to meet the requirements of displaying a file m=
inus the last three=C2=A0lines. It seems portable as well: most unix-ish sy=
stems=C2=A0should have these utilities. I tried=C2=A0to avoid any regexes=
=E2=80=A6as the saying goes, adding a regex means now you have *two* proble=
ms. At least for me it seems to go that way.=C2=A0<div><br></div><div>expor=
t COUNT=3D`wc -l /var/log/messages | tr -d -c &#39;\n[:digit:]&#39;` # get =
the number of lines in the file: if needed/the filename has digits=C2=A0use=
 &#39;cut -f1 -d &quot;/&quot; to isolate<br>export WANT=3D`echo &quot;$COU=
NT-3&quot; | bc ` # subtract 3 (or however many)<br>head -$WANT /var/log/me=
ssages # display the remainder.=C2=A0<br></div><div><br></div><div>might se=
em inefficient=C2=A0but when a raspberry =CF=80 is clocked in GHz, who care=
s?=C2=A0</div></div><br><div class=3D"gmail_quote"><div dir=3D"ltr" class=
=3D"gmail_attr">On Sat, Sep 2, 2023 at 9:27=E2=80=AFPM &lt;<a href=3D"mailt=
o:little.analyst892@aceecat.org">little.analyst892@aceecat.org</a>&gt; wrot=
e:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0=
.8ex;border-left-width:1px;border-left-style:solid;border-left-color:rgb(20=
4,204,204);padding-left:1ex">On Fri, Sep 01, 2023 at 10:43:46AM +0200, Ede =
Wolf wrote:<br>
<br>
&gt; From a file/output with an unknown amount of lines, I would like to fi=
lter<br>
&gt; out, or not display, the last 3 lines. Is there a way to archive this?=
<br>
<br>
Here&#39;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 =3D 0;<br>
@main::queue =3D ();<br>
$main::num_dropped =3D 10;<br>
<br>
sub read_one {<br>
=C2=A0 =C2=A0 my ($next_input) =3D @_;<br>
=C2=A0 =C2=A0 our (@queue, $num_dropped);<br>
=C2=A0 =C2=A0 if ($#queue + 1 &gt;=3D $num_dropped) {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 my $next_output =3D shift @queue;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 print($next_output);<br>
=C2=A0 =C2=A0 }<br>
=C2=A0 =C2=A0 push(@queue, $next_input);<br>
}<br>
<br>
sub usage {<br>
=C2=A0 print STDERR (&quot;usage: notail [-c|--char_mode] [-n|--num_dropped=
 N] [FILE ..]\n&quot;);<br>
=C2=A0 exit(2);<br>
}<br>
<br>
sub main {<br>
=C2=A0 =C2=A0 our ($char_mode, $num_dropped);<br>
=C2=A0 =C2=A0 GetOptions<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (&quot;char_mode&quot; =3D&gt; \$char_mode,<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0&quot;num_dropped=3Di&quot; =3D&gt; \$num=
_dropped)<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 or usage();<br>
=C2=A0 =C2=A0 $/ =3D \1 if $char_mode;<br>
=C2=A0 =C2=A0 while (&lt;&gt;) {<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 read_one($_);<br>
=C2=A0 =C2=A0 }<br>
}<br>
<br>
exit(main());<br>
<br>
1;<br>
__END__<br>
<br>
<br>
<br>
-- <br>
Ian<br>
<br>
</blockquote></div><br clear=3D"all"><div><br></div><span class=3D"gmail_si=
gnature_prefix">-- </span><br><div dir=3D"ltr" class=3D"gmail_signature">Pa=
ul Beard / <a href=3D"http://www.paulbeard.org/" target=3D"_blank">www.paul=
beard.org/</a><br></div>

--00000000000079f4cc06047aea6e--



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