Printing the last occurrence of a string and everything below it with grep
So far the simplest way find the last occurrence of a string and print everything below it is with a combination of grep and tac (it’s cat backwards, get it?!).
Say you want to find the last instance of foo and everything that came after it in a file:
tac /var/log/logfile | grep 'foo' -m 1 -B 9999 | tac
This flips the logfile upside and pipes it to grep, who finds only the first occurrence and prints everything above it (well, 9999 lines), then flips that output right side up again. Works great but I wonder if there is a simpler way to do this.
May 7th, 2010 at 5:07 am
[...] Printing the last occurrence of a string and everything below it with grep › Backdrift [...]