Linux – any way to run grep backwards, i.e. from the end of the file and up

bashcommand linegreplinux

I have a 70G+ log file, and i'd like the most recent entries (apache log append new items at the end) that match a pattern. i can either:

run grep | tail

or

run tail | grep

Option 1 will take forever. Option 2 may return nothing, then I will have to increase the count for tail and keep running until I get something.

If I could grep from the last line up to the first, it would be ideal. But I could not find any option on grep's man page.

Is there any trick to do that? either on grep alone or with any other combination of linux tools?

Best Answer

I think the command that will best help you is tac: http://linux.die.net/man/1/tac

As it states:
tac - concatenate and print files in reverse

So you could pipe it to grep and match nnn number of lines before stopping, or something along those lines.

Related Question