start page | rating of books | rating of authors | reviews | copyrights

Unix Power ToolsUnix Power ToolsSearch this book

13.8. GNU Context greps

By default, standard grep utilities show only the lines of text that match the search pattern. Sometimes, though, you need to see the matching line's context: the lines before or after the matching line. The GNU greps (grep, fgrep, and egrep) can do this. There are three context grep options:

Each set of contiguous matching lines is separated by a line of two dashes (--).

Let's look at an example: I'd like to search my system mail log for all messages sent to anyone at oreilly.com. But sendmail doesn't put all information about a message on the to= log line; some info is in the from= line, which is usually the previous line. So I'll search for all "to" lines and add one line of context before each match. I'll also use the -n, which numbers the output lines, to make the context easier to see. This option also puts marker characters after the line number: a line number ends with a colon (:) if this line contains a match, and a dash (-) marks lines before or after a match. Here goes:

# grep -n -B 1 'to=<[^@]*@oreilly\.com>' maillog
7-Nov 12 18:57:42 jpeek sendmail[30148]: SAA30148: from=<[email protected]>...
8:Nov 12 18:57:43 jpeek sendmail[30150]: SAA30148: to=<[email protected]>...
9-Nov 12 22:49:51 jpeek sendmail[1901]: WAA01901: from=<[email protected]>...
10:Nov 12 22:49:51 jpeek sendmail[1901]: WAA01901: to=<[email protected]>...
11:Nov 12 22:50:23 jpeek sendmail[2000]: WAA01901: to=<[email protected]>...
--
25-Nov 13 07:42:38 jpeek sendmail[9408]: HAA09408: from=<[email protected]>...
26:Nov 13 07:42:44 jpeek sendmail[9410]: HAA09408: to=<[email protected]>...
27-Nov 13 08:08:36 jpeek sendmail[10004]: IAA10004: from=<[email protected]>...
28:Nov 13 08:08:37 jpeek sendmail[10006]: IAA10004: to=<[email protected]>...
--
32-Nov 13 11:59:46 jpeek sendmail[14473]: LAA14473: from=<[email protected]>...
33:Nov 13 11:59:47 jpeek sendmail[14475]: LAA14473: to=<[email protected]>...
34-Nov 13 15:34:17 jpeek sendmail[18272]: PAA18272: from=<[email protected]>...
35:Nov 13 15:34:19 jpeek sendmail[18274]: PAA18272: to=<[email protected]>...

I've truncated each line for printing, but you still can see the matches. A few notes about what's happening here:

-- JP



Library Navigation Links

Copyright © 2003 O'Reilly & Associates. All rights reserved.