Word-oriented GREP

Brian Fitzgerald fitz at mml0.meche.rpi.edu
Mon Apr 15 14:25:20 AEST 1991


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

Try:

sed -n -e 's/.*\(V[0-9][0-9][0-9]\).*/\1/p'

(use \[ and \] in csh)

This just gets the first occurrence on a line, though.  Wizards know
how to print more than one occurrence with shell commands, even if they
are not separated by white space. (i.e. V001,V002,...)

Since I'm not a wizard, I tried lex:

%%
V[0-9][0-9][0-9]	printf("%s\n",yytext);
.	|
\n	;

Save as foo.l and type "make LDLIBS=-ll foo" (or "lex -t foo.l >
foo.c ; cc -o foo foo.c -ll")

Brian



More information about the Comp.unix.questions mailing list