Operating System - HP-UX
1833788 Members
2302 Online
110063 Solutions
New Discussion

Re: regular expression equivalent to perl's '\b' tag to match word

 
SOLVED
Go to solution
Gary Yu
Super Advisor

regular expression equivalent to perl's '\b' tag to match word

Hi all,

I'm trying to find a regular expression tag to match a single word, just like '\b' in perl, so that I can match, say, "good" not "goods".

BTW, grep -w seemed not exists on 11.0

thanks,
Gary
8 REPLIES 8
Brian Bergstrand
Honored Contributor
Solution

Re: regular expression equivalent to perl's '\b' tag to match word

I believe gnu grep can handle PRE's \b. vi and ex can use \ to match a word. But for std. grep you'll have to make up your matching rules.

HTH.
Gary Yu
Super Advisor

Re: regular expression equivalent to perl's '\b' tag to match word

thanks Brian,

I also have the impression that \ should be the one, but I tried it with "sed" and "grep", not good....
Mark Grant
Honored Contributor

Re: regular expression equivalent to perl's '\b' tag to match word

As far as I know, the \b matches a word bounday in perl so I though maybe you could use[^a-Z]

I might, however be misinterpreting what you mean here.
Never preceed any demonstration with anything more predictive than "watch this"
Graham Cameron_1
Honored Contributor

Re: regular expression equivalent to perl's '\b' tag to match word

Not so easy without perl.
You can look for whitespace...
--
#echo goods|grep -E good[[:space:]]
--
and you can look for end of line...
--
#echo goods|grep -E good$
--
and you can combine ...
--
#echo "1goods\n2good\n3good "|grep -E -e good$ -e good[[:space:]]
2good
3good
--
but it starts to get messy.
-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
Gary Yu
Super Advisor

Re: regular expression equivalent to perl's '\b' tag to match word

like Graham said, here's the messy solution:

egrep '[[:space:]]good[[:space:].]|^good[[:space:].]|[[:space:]]good$' some_file

note the dot inside the [], it stands for period, just in case the word appears at the end of a sentence.

but that still doesn't apply to "sed", since it's extended regular expression, I still can not substitute "good" to "VERY GOOD" with "sed"

thanks,
Gary
Mark Grant
Honored Contributor

Re: regular expression equivalent to perl's '\b' tag to match word

Still a touch confused here so excuse me if this doesn't make sense but I think, at least for the grep case, you can do

egrep '[^a-Z]good[^a-Z]' goodfile

I tried this with said but can't get it to work perhaps some better sed maestro can.
Never preceed any demonstration with anything more predictive than "watch this"
Gary Yu
Super Advisor

Re: regular expression equivalent to perl's '\b' tag to match word

Hi Mark,
---
your solution seems doesn't cover the case while the word is at the very begining or very end of a line (^word or word$)
---
and BTW, seemed that the range should be [A-z], not [a-Z](I got such error "egrep: Invalid range within [] expression." )
---
thanks,
Gary
Mark Grant
Honored Contributor

Re: regular expression equivalent to perl's '\b' tag to match word

Ok then,

This one really does work :)

cat goodfile | perl -n -e 's/\bgood\b/VERY good/;print;'
Never preceed any demonstration with anything more predictive than "watch this"