[comp.unix.questions] delimiting a word for grep

rhartman@thestepchild.sgi.com (Robert Hartman) (05/02/91)

In article <26716@adm.brl.mil> RSS%CALSTATE.bitnet@vm.usc.edu (Richard S. Smith) writes:
>I get the feeling there's no good answer to this question, but I
>am asking it anyway...
>
>Is there a SIMPLE, NON-PAINFUL way to set up a regular expression so
>that it will match a given string only when it occurs as a word ...
>
>I am hoping there is a simpler answer than:
>
>"[^A-Za-z0-9]foo[^A-Za-z0-9]"

Well, if you have SunOS 4.x, I believe you can use the '\<' and '\>' word
delimiters, just like in vi.  Some other versions of grep may also support
this.  Worth a try!  If your grep doesn't like this, then maybe your sed will:

	$ sed -n '/\<foo\>/p' file

Otherwise, I'd use a shell variable to hold the grot if you're going to do
that very often:

	$ gwd='[^a-zA-Z0-9-_]'
	$ grep "${gwd}foo${gwd}" file

-r