Re: cut off last lines of a document
- In reply to: Ede Wolf : "cut off last lines of a document"
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Fri, 01 Sep 2023 12:12:13 UTC
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 .