Word-oriented GREP

kevin lyda lyda at acsu.buffalo.edu
Mon Apr 29 03:08:59 AEST 1991


In article <b08Gc5!p1 at cs.psu.edu> flee at cs.psu.edu (Felix Lee) writes:
.(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

.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.)

why not cut down your search space by using grep to find the lines with
the matching patterns and then using perl, or some other unix tool to grab
the pattern.... from the previous example you could do:

grep V\[0-9\]\[0-9\]\[0-9\] fred.c | tr ' ' \012 | grep V\[0-9\]\[0-9\]\[0-9\]

of course that assumes that your field separators are spaces.

	a non-wizard,
		kevin



More information about the Comp.unix.wizards mailing list