I've been using
awk '/KEYWORD/{print $0}' FILENAME
to filter a file, how do I modify the command so as to print the last n lines only?
I've been using
awk '/KEYWORD/{print $0}' FILENAME
to filter a file, how do I modify the command so as to print the last n lines only?
To do this in awk
only, without additional filtering through tail
or whatever, we can build an N element FIFO buffer into which we put all the matches. Then we just dump the relevant content from the buffer when done.
BEGIN { N = 10; i = 0 }
/keyword/ { fifo[i] = $0; i = (i + 1) % N }
END { for (x = -N; x < 0; x++)
{ j = (i + x + N) % N
if (fifo[j])
print fifo[j] } }