With grep -w
, you are asking grep
to only return matches that are immediately preceded by or followed by a newline (at the very start or end of a line) or a non-word character (a word character is a letter, digit or underscore).
This means that the text this is flag{a}
will not be matched by grep -w 'flag{
', since the character following the {
in the text is a word character.
Similarly, this is theflag{
would not be matched by grep -w 'flag{'
.
You may dispose of the -w
option and instead use an explicit zero-width word boundary pattern, for example at the beginning, so that you may match flag{a}
but not theflag{
. With GNU grep
, this may be done using
grep -rn '\<flag{' Downloads
or
grep -rn '\bflag{' Downloads
The \<
pattern matches a word boundary at the start of a word (and \>
matches at the end), while \b
matches at both start and end. \<
and \>
were originally inherited from the ex
and vi
editors and are more portable than \b
.
GNU grep
does not support [[:<:]]
and [[:>:]]
(which work the same way as \<
and \>
) to match at the beginning or end of words, but BSD grep
does. BSD grep
does not support \b
.
{
.