display whole stream while highlighting with grep

1

I often use tail -f | grep someregex on log files. I have grep liased to grep ---color=auto so my matches are highlighted. If I want to see my matches in the context of the whole stream, how would I go about that? Essentially, I want to use tail + grep as a highlighter.

Pete

Posted 2013-11-13T19:46:34.310

Reputation: 277

1Could you please clarify? What do you mean by the whole stream? If you want the whole stream, just don't use tail, run grep somepattern directly. Could you give an example of the kind of process you are monitoring? – terdon – 2013-11-13T19:49:15.847

if I am tailing a log file, grepping on error messages, but want to see it in context of some debug messages leading up to that match, in real time. – Pete – 2013-11-13T19:54:00.157

Answers

2

To see your matches in context, use the -C option. To see all the lines, use a huge argument to -C, e.g.,

tail -f yourlogfile | grep -C9999 someregex

garyjohn

Posted 2013-11-13T19:46:34.310

Reputation: 29 085

Not sure why I didn't try this, I just assumed the context arguments wouldn't work on streams. Thanks. – Pete – 2013-11-13T20:05:47.667

0

If I understand well, it is what you are looking for:

grep "someregex" yourFile

It will print the lines where the "regex" matches.

Manolo

Posted 2013-11-13T19:46:34.310

Reputation: 335

I'm using this on a log file, I would like to see it output in real time w/o interaction, hence the tail -f, but want to highlight matches – Pete – 2013-11-13T19:57:15.403

The matches are highlighted with your command. So what is your desired output? – Manolo – 2013-11-13T20:01:24.800

Well, I think I understand you now, since I've seen @garyjohn answer. Is it your desired behavior? – Manolo – 2013-11-13T20:04:26.577