Printing the last occurrence of a string and everything below it with grep

Tags: , ,

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.

One Response to “Printing the last occurrence of a string and everything below it with grep”

  1. 5 Easy Ways to Avoid Computer Problems | Gadgets Deconstructed Says:

    [...] Printing the last occurrence of a string and everything below it with grep › Backdrift [...]

Join the Conversation