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

8,284

Solution 1

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.

Solution 2

That's a big file. You should rotate those logs more often.

If tac is too slow, you could pick a programming language with a seek command (perl, for instance), then:

  • open the file
  • seek to the end
  • iteratively:
    • seek backwards some amount (4K, or larger)
    • read that amount of text
    • split on newlines, and search for whatever.
Share:
8,284

Related videos on Youtube

gcb
Author by

gcb

Updated on September 18, 2022

Comments

  • gcb
    gcb over 1 year

    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?

    • Daniel B
      Daniel B over 9 years
      Are line numbers important?