[One weakness of the grep
family of programs is that they are
They read only one line at a time, and so they can't
find patterns (such as phrases) that are split across two lines.
can do multiline searches.
One advantage of the cgrep.sed
script is that it shows
how to handle multiple-line patterns in sed
-and can be adapted
for work other than searches. -JP ]
It may surprise you to learn that a fairly decent context grep
program can be built using
version of cgrep
is used the same way as the Perl version
cgrep -10 system main.c
will find all lines containing the word system
in the file main.c
and show 10 additional lines of context above and below each match.
option must be at least 1, and defaults to 2 lines.)
It differs from the Perl version in that, if several matches occur
within the same context, the lines are printed as one large "hunk"
rather than repeated smaller hunks.
Each new block of context is
preceded by the line number of the first occurrence in that hunk.
This script can also search for patterns that span lines:
cgrep -3 "awk.*perl"
will find all occurrences of the word "awk" where it is followed by
the word "perl" somewhere within the next 3 lines.
The pattern can
simple regular expression (26.4
with one notable exception:
because you can match across lines, you should use
in place of the
explains how the script works.