Is it possible to use grep to pick up only full words?

 -w, --word-regexp
              Select  only  those  lines  containing  matches  that form whole
              words.  The test is that the matching substring must  either  be
              at  the  beginning  of  the  line,  or  preceded  by  a non-word
              constituent character.  Similarly, it must be either at the  end
              of  the  line  or  followed by a non-word constituent character.
              Word-constituent  characters  are  letters,  digits,   and   the
              underscore.

from man grep


Also you can use this:

echo "this is the theater" |grep --color '\bthe\b'

For one word is the same with -w.
But if you need to search multiple patterns you can use the \b, otherwise all patterns will be treated as words if -w is in use.

For example :

grep -w -e 'the' -e 'lock'

will highlight the and lock but not keylock /padlock etc.

With \b you can treat each -e pattern differently.

Test it here.


You can test the presence of the beginning (resp. end) of a word with the marker \< (resp. \>).

Thus,

grep "\<the\>" << .
the cinema
a cinema
the theater
a theater
breathe
.

gives

the cinema
the theater

Tags:

Linux

Grep