Grep: end of word delimiter not working

grepregular expression

Why in the following (e)grep attempts, egrep is unable to identify the end-of-word delimiter? (b)?

$ echo -n "my-pc is beautiful" | egrep  'my-pc\b'
my-pc is beautiful
/home/pkaramol
$ echo -n "my-pc-vol2 is beautiful" | egrep  'my-pc\b'
my-pc-vol2 is beautiful

Same are the results with plain grep?

Best Answer

It identifies it fine but grep and egrep print the entire line of a match unless the -o option is used:

-o, --only-matching

Prints only the matching part of the lines.

I think you want:

$ echo -n "my-pc is beautiful" | egrep -o 'my-pc\b'
my-pc

A word boundary is any non-word character. The word characters are: [a-zA-Z0-9_]. Therefore - is not a word character.

Related Question