Word-oriented GREP

Randal L. Schwartz merlyn at iwarp.intel.com
Mon Apr 15 14:43:12 AEST 1991


In article <1991Apr15.014626.28903 at berlioz.nsc.com>, nelson at 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 at iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/



More information about the Comp.unix.questions mailing list