Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!ub!lyda From: lyda@acsu.buffalo.edu (kevin lyda) Newsgroups: comp.unix.wizards Subject: Re: Word-oriented GREP Message-ID: <73515@eerie.acsu.Buffalo.EDU> Date: 28 Apr 91 17:08:59 GMT References: <1991Apr15.014626.28903@berlioz.nsc.com> <1991Apr15.044312.27326@iwarp.intel.com> Sender: news@acsu.Buffalo.EDU Distribution: na Organization: State University of New York at Buffalo/Comp Sci Lines: 29 Nntp-Posting-Host: sybil.cs.buffalo.edu In article flee@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