delimiting a word for grep (was: How do I find a word)

Robert Hartman rhartman at thestepchild.sgi.com
Thu May 2 08:46:57 AEST 1991


In article <26716 at adm.brl.mil> RSS%CALSTATE.bitnet at 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



More information about the Comp.unix.questions mailing list