From nobody Mon Sep 4 15:22:32 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 4RfXSh3Mjvz4s7LK for ; Mon, 4 Sep 2023 15:22:44 +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 4RfXSg39Sxz3NZT for ; Mon, 4 Sep 2023 15:22:43 +0000 (UTC) (envelope-from andreas.kahari@abc.se) Authentication-Results: mx1.freebsd.org; dkim=pass header.d=abc.se header.s=default header.b=e1tytBTJ; spf=pass (mx1.freebsd.org: domain of andreas.kahari@abc.se designates 158.174.61.227 as permitted sender) smtp.mailfrom=andreas.kahari@abc.se; dmarc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; s=default; bh=oEZsu0WI6d /uHRgBtuCh2/E9ts3mAjvf0OQiWzYxfzI=; h=in-reply-to:references:subject: to:from:date; d=abc.se; b=e1tytBTJB20ySyaKJMkdGeJFQw11hiXPMmE/PT/xaR4r NV1xxG96iVgg4c8ixGUT0NmKO4SZBRoBQvHJiugqG+eKWizxHIQ2iha4As2WW1S59zdwFr XsAIT/oKUAiMBvExe7ZERFGZiNm9eRTCpVzCD/CZNlocuA2wcp/DjgQJbBFk9yDvTYtvK0 KQmbUTUKS3ZcZ4vibrNaREWco/GURIlEgOkB1vvLYVtkyZleJ6mNwEP31lxSxeHR4JXVeU pYe5Cywm1K2k+qMXJJtIGs/mS9rqav6grH2R7jiAYHBn2aK4tg7+UmU+KuGhj66MKxxzl3 sXfyeRXmKYgtdCzAAA== Received: from harpo.local (94-255-242-14.cust.bredband2.com [94.255.242.14]) by hekla.abc.se (OpenSMTPD) with ESMTPSA id 6c210e42 (TLSv1.3:TLS_AES_256_GCM_SHA384:256:NO) for ; Mon, 4 Sep 2023 17:22:34 +0200 (CEST) Date: Mon, 4 Sep 2023 17:22:32 +0200 From: Andreas Kusalananda =?utf-8?B?S8OkaMOkcmk=?= To: questions@freebsd.org Subject: Re: cut off last lines of a document Message-ID: Mail-Followup-To: questions@freebsd.org References: <57be5495-97f8-4f22-9ae2-cd9712596e64@nebelschwaden.de> <86edjet336.fsf@ltc.des.no> 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: <86edjet336.fsf@ltc.des.no> X-Spamd-Bar: -- X-Spamd-Result: default: False [-2.55 / 15.00]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_MEDIUM(-0.99)[-0.994]; NEURAL_HAM_SHORT(-0.99)[-0.986]; R_MIXED_CHARSET(0.83)[subject]; R_DKIM_ALLOW(-0.20)[abc.se:s=default]; R_SPF_ALLOW(-0.20)[+ip4:158.174.61.227]; MIME_GOOD(-0.10)[text/plain]; ONCE_RECEIVED(0.10)[]; TO_DN_NONE(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; RCVD_COUNT_ONE(0.00)[1]; MIME_TRACE(0.00)[0:+]; ASN(0.00)[asn:8473, ipnet:158.174.0.0/16, country:SE]; ARC_NA(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; MLMMJ_DEST(0.00)[questions@freebsd.org]; DKIM_TRACE(0.00)[abc.se:+]; PREVIOUSLY_DELIVERED(0.00)[questions@freebsd.org]; FROM_HAS_DN(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; DMARC_NA(0.00)[abc.se]; RCVD_TLS_ALL(0.00)[]; RCVD_VIA_SMTP_AUTH(0.00)[] X-Rspamd-Queue-Id: 4RfXSg39Sxz3NZT Sorry, this is resent due to a misconfiguration of my mail client. On Mon, Sep 04, 2023 at 04:39:25PM +0200, Dag-Erling Smørgrav wrote: > Ede Wolf writes: > > Am 03.09.23 um 23:16 schrieb paul beard: > > > export COUNT=`wc -l /var/log/messages | tr -d -c '\n[:digit:]'` # > > > export WANT=`echo "$COUNT-3" | bc ` # subtract 3 (or however many) > > > head -$WANT /var/log/messages # display the remainder. > > 1) there is no need to export the variables > 2) don't use backticks, use $() instead > 3) if you use 'wc -l 3b) alternatively, use read: wc -l foo | read COUNT FILENAME > 4) the shell can do the math for you: 'head -$((COUNT-3)) foo' > > > Thanks, but the problem I currently see here, as with the suggestion > > of Archimedes earlier, I am currently not easily able to convert this > > into a feed from stdin. > > Pipe-friendly pure shell solution: > > drop_last_three() { > local a b c d > read a > read b > read c > while read d ; do > echo "$a" > a="$b" > b="$c" > c="$d" > done > } > > DES > -- > Dag-Erling Smørgrav - des@FreeBSD.org Note how this will handle backslash-escaped newlines in the input, and that, depending on the shell and its configuration, the "echo" call may or may not expand backslash-escaped characters in the input (e.g. "\n" and "\t"). The "echo" may also interpret the string "-n" as an option it occurs on its own line. You may also lose flanking whitespace. -- Andreas (Kusalananda) Kähäri SciLifeLab, NBIS, ICM Uppsala University, Sweden .