merlyn@iwarp.intel.com (Randal L. Schwartz) (04/15/91)
In article <1991Apr15.014626.28903@berlioz.nsc.com>, nelson@berlioz (Taed Nelson) writes: | 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 thought about it for a while, and I couldn't come up with anything; even | AWK seems to offer no nice way of doing it, but this seems like something | that is at least somewhat common... | Does anyone have any suggestions, preferably limiting the solution to SH or | CSH, and by not using uncommon tools such as PERL? It's such a natural task for Perl, as in: print -ne 'print "$&\n" if /V\d\d\d/' fred.c If you have multiple occurrances, you can do the slightly more arcane: print -ne 's/V\d\d\d/print "$&\n"/eg' fred.c By the way, if you find Perl to be "uncommon", I suggest you make yourself more aware of it. It's everywhere these days. For example, there exists at least one computer manufacturer that has it installed at sysgen time, and it's part of the GNU utils tape. And because it's free, and highly (even overly :-) portable, you can put it up yourself if necessary. print "Just another Perl hacker," -- /=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\ | on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III | | merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn | \=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/
flee@cs.psu.edu (Felix Lee) (04/15/91)
(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@cs.psu.edu