Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!sdd.hp.com!elroy.jpl.nasa.gov!decwrl!sgi!shinobu!odin!thestepchild!rhartman From: rhartman@thestepchild.sgi.com (Robert Hartman) Newsgroups: comp.unix.questions Subject: delimiting a word for grep (was: How do I find a word) Message-ID: <1991May1.224657.6520@odin.corp.sgi.com> Date: 1 May 91 22:46:57 GMT References: <26716@adm.brl.mil> Sender: news@odin.corp.sgi.com (Net News) Organization: Silicon Graphics, Inc., Mountain View, CA Lines: 24 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 '/\/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