3

Using sed, grep, awk, or ruby, what's a simple way to find the last match of a string in a larger log file, and send every line following it (inclusive) to another file?

Example:

grep -A222 "_____________" recent/toptrigs.OX.rb.log 

is my nearest workaround, since 222 lines of context afterword should always hit EOF first, except that I want it to start from the last instance of "_____" string. It's a logfile simultaneously growing from the same process (so I manually flush STDOUT and STDERR at the end of each item's loop, with this segment cutting in mind).

1
  • Prefer a regex-based solution (avoiding double-tac on a large file in a fast loop) Commented Dec 12, 2012 at 10:50

2 Answers 2

5

I think the easiest way is to reverse the input before matching:

<logfile tac | sed '/pattern/q' | tac 
2
  • I was trying to avoid that too since the main logfile grows many MB's huge, and the iterations fast (don't want to slow them down by a large fraction), so was looking for a regex-only solution. Commented Dec 12, 2012 at 10:41
  • @Marcos: no matter the solution it needs to determine the offset to your pattern from the end-of-file. Based on my non-exhaustive experiments the solution I suggested seems to be the most efficient. Commented Dec 12, 2012 at 11:16
2

Some options here.

  • You could utilize tac to reverse the order of lines, then making grep include the line after (now before as it's reversed) and stop it after the first match, then reverse it again to restore the order, like this:

    tac /path/to/file | grep -m 1 -B 1 "mypattern" | tac 
  • Strip out lines besides the last match and the line following:

    grep -A 1 "mypattern" /path/to/file | tail -n 2 

You should try this on your files to see which performs better in your case.

And you can append the output to a log file by prepending the commands with standard output redirection >> /path/to/outputfile.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.