Grep word boundaries

Accorging to GNU documentation:

‘<’ Match the empty string at the beginning of word.
‘>’ Match the empty string at the end of word.

My /etc/fstab looks like this:

/dev/sdb1       /media/fresh      ext2   defaults     0 0

I want grep to return TRUE/FALSE for the existence of /media/fresh. I tried to use < and > but it didn’t work. Why?

egrep '</media/fresh>' /etc/fstab

Workaround:

egrep '[[:blank:]]/media/fresh[[:blank:]]' /etc/fstab

But it looks uglier.

My grep is 2.5.1

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

< 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

Method 2

This problem with < (and alsob) 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)"


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x