Grep word boundaries
\<
and \>
match empty string at the begin and end of a word respectively and only word constituent characters are:
[[:alnum:]_]
From man grep
:
Word-constituent characters are letters, digits, and the underscore.
So, your Regex is failing because /
is not a valid word constituent character.
Instead as you have spaces around, you can use -w
option of grep
to match a word:
grep -wo '/media/fresh' /etc/fstab
Example:
$ grep -wo '/media/fresh' <<< '/dev/sdb1 /media/fresh ext2 defaults 0 0'
/media/fresh
This problem with \<
(and also\b
) applies not only to /
, but to all non-word characters. (i.e. characters other than [[:alnum:]]
and _
. )
The problem is that the regex engine will always bypass a non-word character like /
when searching for the next anchor \<
.
That's why you should not put non-word characters like /
right after \<
.
If you do, by construction, nothing will match.
An alternative to the -w
option of grep, would be something like this:
egrep "(^|\W)/media/fresh($|\W)"