Re: cut off last lines of a document
Date: Mon, 04 Sep 2023 14:39:25 UTC
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