From nobody Fri Sep 01 12:12:13 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 4RccPC4H1tz4sQTJ for ; Fri, 1 Sep 2023 12:13:03 +0000 (UTC) (envelope-from andreas.kahari@abc.se) Received: from hekla.abc.se (hekla.abc.se [158.174.61.227]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA512) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 4RccPB6lKxz4JXZ for ; Fri, 1 Sep 2023 12:13:02 +0000 (UTC) (envelope-from andreas.kahari@abc.se) Authentication-Results: mx1.freebsd.org; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; s=default; bh=AUi+3QLng8 vBqmyQGSOmQX4OUGJk7Swf/D0f8N8baRY=; h=in-reply-to:references:subject: cc:to:from:date; d=abc.se; b=BuRIGaVw0jAk6hZYFxnCOevtLgsvQSnGecl261ftt Czrfd3w8Mhg/YqVhM8cbs+8FVzaQeUauyGHC+lgBUG3vB4S0t2DtvWzVLfQIV83yBf16+E fwjbVVaXf88T4zewyBrKTBKNYjN94P86khwZl/TupmCxOwy1y8LFAaSvabeUZbm3d+wYi6 9p1QDYzUC3y612riMXBgcnjXQmdsZRRejYUldrlmakrHA1DtddcuZsQIpQehYWW28QHm47 TvPZeZT6Ay+0O2rEeVC+b3tOM5zhxSNy4EHxVRm3WyYtAdedvbWz73NLwez+vGdyRvxT1U TLQZfsRN6iMjUIkPsLRRw== Received: from harpo.local (94-255-242-14.cust.bredband2.com [94.255.242.14]) by hekla.abc.se (OpenSMTPD) with ESMTPSA id 5b053544 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO); Fri, 1 Sep 2023 14:12:15 +0200 (CEST) Date: Fri, 1 Sep 2023 14:12:13 +0200 From: Andreas Kusalananda =?utf-8?B?S8OkaMOkcmk=?= To: Ede Wolf Cc: User Questions Subject: Re: cut off last lines of a document Message-ID: Mail-Followup-To: Ede Wolf , User Questions References: <57be5495-97f8-4f22-9ae2-cd9712596e64@nebelschwaden.de> 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 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <57be5495-97f8-4f22-9ae2-cd9712596e64@nebelschwaden.de> X-Spamd-Bar: ---- X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Spamd-Result: default: False [-4.00 / 15.00]; REPLY(-4.00)[]; ASN(0.00)[asn:8473, ipnet:158.174.0.0/16, country:SE] X-Rspamd-Queue-Id: 4RccPB6lKxz4JXZ On Fri, Sep 01, 2023 at 10:43:46AM +0200, Ede Wolf 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? > > For those also using linux, there is a "head -n -3" for this. That negative > number option is not available on FreeBSD, but maybe there is an alterative > way of doing this? Preferably /bin/sh compatible. > > Thanks > > Ede Using awk: awk -v n=3 'NR > n { print buffer[NR%n] } { buffer[NR%n] = $0 }' This will print all lines except the last "n" lines. The buffer array is a circular buffer that contains the most recently read "n" lines. The code outputs nothing until the first "n" lines have been read, then it outputs lines from the buffer array in a circular fashion while at the same time filling the array with new lines from the input. -- Andreas (Kusalananda) Kähäri SciLifeLab, NBIS, ICM Uppsala University, Sweden .