Re: cut off last lines of a document

From: Tom Browder <tom.browder_at_gmail.com>
Date: Fri, 08 Sep 2023 00:02:13 UTC
On Fri, Sep 1, 2023 at 05:23 Ede Wolf <listac@nebelschwaden.de> wrote:
> Hello,
>  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?

Ede, I would like to know your use case. You have had lots of suggestions
of arcane shell scripting solutions for use on any Unix-like host, but I
want to suggest an alternative which is even more universal: use a Raku
script (see the solution below). Let me explain.

Short version
==========

In any line of work, I believe you should automate any work you can to give
more time for other tasks.  For the work that can be automated by shell
scripting, Raku provides easier coding and easier maintenance.

Long version
==========

In my younger days I was a semipro sysadmin for years with a DoD
contractor. I introduced Linux for our CAD developers when we were still
using SGI workstations that cost about $25K and $6K annually to support.

I introduced the old Yggdrasil Linux installed on a small IBM PC as a much
cheaper alternative as we started weaning ourselves off big iron and
providing all analysts with his or her own workstation. Over the years I
used Perl to do more complicated sysadmin support for my team of analysts
and moved away csh to bash in the process. Note Perl is now available by
default on the Linux distros I’m familiar with , but I have been using Raku
as its replacement since 2015 and, while not automatically available, it is
easily installable as packaged in major Linux distros as well as for
NetBSD. Other installation support is provided for Windows and MacOS. See <
rakudo.org> for details.

RAKU SOLUTION

So, with Raku installed, I view a file “somefile” in a terminal with a Raku
one-liner like this at the shell prompt:

$ raku -e 'say $_ for "somefile".IO.lines[^(*-3)]'
...all but the last three lines scroll by...

(Note I would make the one-liner reusable by putting that line in a bash
script file with a suitable and meaningful name, something like
"cat-less-3-lines.sh."

Happy rakuing!


-Tom