5

Simply put:

echo "xxxxx Tyyy zzzzz" | egrep "\byyy\b" 

(no match which is correct)

echo "xxxxx T-yyy zzzzz" | egrep "\byyy\b" 
xxxxx T-yyy zzzzz

I dont want it to match like it does in the second expression, please advise how I can achieve this, thanks.

Jahed Uddin
  • 301
  • 1
  • 10

1 Answers1

4

You can use:

echo "xxxxx T-yyy zzzzz" | grep -E "(^|[^-])\byyy\b([^-]|$)"

Where (^|[^-])\byyy\b([^-]|$) will match start or non-hyphen on LHS and end or non-hyphen on RHS of the matched word yyy.

anubhava
  • 761,203
  • 64
  • 569
  • 643