From nobody Sun Sep 03 21:16:54 2023 X-Original-To: questions@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Rf4N56qcxz4rNRs for ; Sun, 3 Sep 2023 21:17:09 +0000 (UTC) (envelope-from paulbeard@gmail.com) Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1D4" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4Rf4N50FWMz3DrN for ; Sun, 3 Sep 2023 21:17:09 +0000 (UTC) (envelope-from paulbeard@gmail.com) Authentication-Results: mx1.freebsd.org; dkim=pass header.d=gmail.com header.s=20221208 header.b=XI5Mgvef; spf=pass (mx1.freebsd.org: domain of paulbeard@gmail.com designates 2a00:1450:4864:20::631 as permitted sender) smtp.mailfrom=paulbeard@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-ej1-x631.google.com with SMTP id a640c23a62f3a-99bf3f59905so126479866b.3 for ; Sun, 03 Sep 2023 14:17:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1693775827; x=1694380627; darn=freebsd.org; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :from:to:cc:subject:date:message-id:reply-to; bh=9bVDtDWS8tvJJP5+tqHqo8Yk2mmL5gHc90oHebZEA6g=; b=XI5MgvefWwVnlf6fDR60xBiqPy51kyEeGS13wBfZ8lpFcnLX2p3GHZ/b/Z7ivDGQGq ELoNaOf7UApKVb0QOH1mCePflmxmb6ewSVGRQswDjr7k2XF+QcSYzXbrXGO9CLasUW2d R8ufswSkn+Sv7xv7TYD5cgqNfYi8ipVMcv2OFdlB5m+4lYh4c4c52CyxFy3HpCfuQCaO f6tNGJNC7Shc/E7g8KCxVcFkHrg1M/UKfVfRQxa+zZfZOMBRqksnUDuXhlIrcYapS9ZJ 27j8k0b9GlYvVQ9QUruja1nujCf3gi/mjIFjmfJgYw/obMUSV+ALy1X3zlT/DseARpjh gNjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693775827; x=1694380627; h=to:subject:message-id:date:from:in-reply-to:references:mime-version :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=9bVDtDWS8tvJJP5+tqHqo8Yk2mmL5gHc90oHebZEA6g=; b=aGsIqAqqoKOqujHZUP/n9wWPckwbv7EB6qBiaf/mLqr84hnmbdCsQRxmkn1wcFDAHF 5haROs33a3HmRD2SJ+1dF2fBQh5KwHQLhoe9F5EihZuZp4XmcagSIuBzBwUzJv7jFSo1 xqFFjigs2+qELCQBy+Hr8DDSX8yYL/VCCTa3qUrenfrdLwoqY7UYxJzfv+loz5PUhJyw gi2pZCqMB11Fi2BJ1nkfhn0JHLvHzZ2o0xcFivh+ismKH/+Oazd8kCyrQQb05jNS56wq VxvIANbL0sLJi4hk9BzeMhyOqquL90NkM9BygAW6hHnW8cA9uUkPZcdd13dYMPcNpNlt mlrw== X-Gm-Message-State: AOJu0YzVmkiZ07ko4TAAlBN1QhhmiDj8b54Skd5aI+6NrNveKmNCDmxY YoNWqrHlYlwB9C+8ylb6+ql2TC6RK5uAHVViCj5d2OudHCs= X-Google-Smtp-Source: AGHT+IFQxiLiTNJL3lZu/lMYYKiJu7sndWNI2M3OKoqXkzbknOXNbGdqPVUqOEDWKl/O7zFt1iDGX+++Iaerq7sFcOY= X-Received: by 2002:a17:906:5d:b0:9a1:c991:a514 with SMTP id 29-20020a170906005d00b009a1c991a514mr6902384ejg.63.1693775826644; Sun, 03 Sep 2023 14:17:06 -0700 (PDT) List-Id: User questions List-Archive: https://lists.freebsd.org/archives/freebsd-questions List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-questions@freebsd.org X-BeenThere: freebsd-questions@freebsd.org MIME-Version: 1.0 References: <57be5495-97f8-4f22-9ae2-cd9712596e64@nebelschwaden.de> In-Reply-To: From: paul beard Date: Sun, 3 Sep 2023 14:16:54 -0700 Message-ID: Subject: Re: cut off last lines of a document To: questions@freebsd.org Content-Type: multipart/alternative; boundary="00000000000079f4cc06047aea6e" X-Spamd-Bar: --- X-Spamd-Result: default: False [-3.45 / 15.00]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; NEURAL_HAM_LONG(-1.00)[-1.000]; DMARC_POLICY_ALLOW(-0.50)[gmail.com,none]; NEURAL_HAM_SHORT(-0.45)[-0.450]; R_DKIM_ALLOW(-0.20)[gmail.com:s=20221208]; R_SPF_ALLOW(-0.20)[+ip6:2a00:1450:4000::/36]; MIME_GOOD(-0.10)[multipart/alternative,text/plain]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_HAS_DN(0.00)[]; FREEMAIL_FROM(0.00)[gmail.com]; RCPT_COUNT_ONE(0.00)[1]; PREVIOUSLY_DELIVERED(0.00)[questions@freebsd.org]; BLOCKLISTDE_FAIL(0.00)[2a00:1450:4864:20::631:server fail]; ARC_NA(0.00)[]; RCVD_TLS_LAST(0.00)[]; RCVD_IN_DNSWL_NONE(0.00)[2a00:1450:4864:20::631:from]; DKIM_TRACE(0.00)[gmail.com:+]; MID_RHS_MATCH_FROMTLD(0.00)[]; TO_DN_NONE(0.00)[]; DWL_DNSWL_NONE(0.00)[gmail.com:dkim]; RCVD_COUNT_ONE(0.00)[1]; MIME_TRACE(0.00)[0:+,1:+,2:~]; FROM_EQ_ENVFROM(0.00)[]; ASN(0.00)[asn:15169, ipnet:2a00:1450::/32, country:US]; FREEMAIL_ENVFROM(0.00)[gmail.com]; MLMMJ_DEST(0.00)[questions@freebsd.org] X-Rspamd-Queue-Id: 4Rf4N50FWMz3DrN --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 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
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

expor= t 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=C2=A0use= 'cut -f1 -d "/" to isolate
export WANT=3D`echo "$COU= NT-3" | bc ` # subtract 3 (or however many)
head -$WANT /var/log/me= ssages # display the remainder.=C2=A0

might se= em inefficient=C2=A0but when a raspberry =CF=80 is clocked in GHz, who care= s?=C2=A0

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 fi= lter
> 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 {
=C2=A0 =C2=A0 my ($next_input) =3D @_;
=C2=A0 =C2=A0 our (@queue, $num_dropped);
=C2=A0 =C2=A0 if ($#queue + 1 >=3D $num_dropped) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 my $next_output =3D shift @queue;
=C2=A0 =C2=A0 =C2=A0 =C2=A0 print($next_output);
=C2=A0 =C2=A0 }
=C2=A0 =C2=A0 push(@queue, $next_input);
}

sub usage {
=C2=A0 print STDERR ("usage: notail [-c|--char_mode] [-n|--num_dropped= N] [FILE ..]\n");
=C2=A0 exit(2);
}

sub main {
=C2=A0 =C2=A0 our ($char_mode, $num_dropped);
=C2=A0 =C2=A0 GetOptions
=C2=A0 =C2=A0 =C2=A0 =C2=A0 ("char_mode" =3D> \$char_mode,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0"num_dropped=3Di" =3D> \$num= _dropped)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 or usage();
=C2=A0 =C2=A0 $/ =3D \1 if $char_mode;
=C2=A0 =C2=A0 while (<>) {
=C2=A0 =C2=A0 =C2=A0 =C2=A0 read_one($_);
=C2=A0 =C2=A0 }
}

exit(main());

1;
__END__



--
Ian



--
Pa= ul Beard / www.paul= beard.org/
--00000000000079f4cc06047aea6e--