bash: hwo to know NUM option in grep -A -B "on the fly" ?

Posted by Michael Mao on Stack Overflow See other posts from Stack Overflow or by Michael Mao
Published on 2010-04-16T00:35:49Z Indexed on 2010/04/16 0:43 UTC
Read the original article Hit count: 277

Filed under:
|

Hello everyone:

I am trying to analyze my agent results from a collection of 20 txt files here.

If you wonder about the background info, please go see my page, what I am doing here is just one step.

Basically I would like to take only my agent's result out of the messy context, so I've got this command for a single file:

cat run15.txt | grep -A 50 -E '^Agent Name: agent10479475' | grep -B 50 '^=='

This means : after the regex match, continue forward by 50 lines, stop, then match a line separator starts with "==", go back by 50 lines, if possible (This would certainly clash the very first line).

This approach depends on the fact that the hard-coded line number counter 50, would be just fine to get exactly one line separator.

And this would not work if I do the following code:

cat run*.txt | grep -A 50 -E '^Agent Name: agent10479475' | grep -B 50 '^=='

The output would be a mess...

My question is: how to make sure grep knows exactly when to stop going forward, and when to stop getting backward?

Any suggestion or hint is much appreciated.

© Stack Overflow or respective owner

Related posts about bash

Related posts about grep