Tuesday, August 2, 2011

word boundary in grep shell

In the case of the perl we use "\b" to specify the word boundary.i.e. in the case of perl
if we want to match a word hello we can use /\bhello\b/ which matches word hello as a separate word or when it occurs as the first word without being combined with someother word.

In Shell if we want to match a word in a word boundary using grep we can use "-w" option of grep which matches the word in a word context.
Using
grep -w "hello" filename 

is same as

grep "\<hello\>" filename

\<  - starts a word boundary.
\>  - ends a word boundary.

grep "\<hello" filename will search for hello,hellobuddy,helloname everyname starting with hello but not Maahello since it starts with a word boundary.

No comments:

Post a Comment