I've learnt how to grep
lines before and after the match and to grep
the last match but I haven't discovered how to grep
the last match and the lines underneath it.
The scenario is a server log.
I want to list a dynamic output from a command. The command is likely to be used several times in one server log. So I imagine the match would be the command and somehow grep
can use -A
with some other flag or variation of a tail command, to complete the outcome I'm seeking.
using awk instead:
small test, find the last line matching
/8/
and the next line of it:The approach I would take it to reverse the problem as it's easier to find the first match and print the context lines. Take the file:
Say we want the last match of
foo
and the following tolines
we could just reverse the file withtac
, find the first match andn
lines above using-Bn
and stop using-m1
. Then simple re-reverse the output with tac:Tools like
tac
andrev
can make problems that seem difficult much easier.