Re: cut off last lines of a document
- In reply to: Dag-Erling_Smørgrav : "Re: cut off last lines of a document"
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Mon, 04 Sep 2023 15:22:32 UTC
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 <listac@nebelschwaden.de> 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 <foo' instead of 'wc -l foo' you don't need tr > 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 .