Word-oriented GREP

Felix Lee flee at cs.psu.edu
Tue Apr 16 01:12:09 AEST 1991


(from comp.unix.questions)
| When I use the command "grep V\[0-9\]\[0-9\]\[0-9\] fred.c" it returns
| 	#define VERSION "V002"
|   or somesuch.  What I would really like is just the string of characters
|   which matched:
| 	V002

I've wanted an "xgrep" tool for a while.  It would scan an input
stream for a pattern and print any part of the stream that matches.

Randal Schwartz offers a Perl solution, but you can't escape line
boundaries.  Consider the pattern
	^(.*\n){0,3}.*Able.*(\n.*){0,3}$
which means, print three lines of context around any line that
contains "Able".  Generalized context grep.  You can write patterns
for any type of simple context.

(You can actually do this in Perl, but it becomes extremely
inefficient for large files, because you can only apply patterns to
strings, not streams.)
--
Felix Lee	flee at cs.psu.edu



More information about the Comp.unix.wizards mailing list