Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using \b with grep pattern

Tags:

I am learning bash and I have come across regular expressions. There is an exercise where I have to match a word and I tried to use \b<word>\b but for some reason it was not matched until I used \\b<word>\\b. I actually tried it out of desperation when I couldn't understand why \b wasn't working.

like image 579
Remi Avan-Nomayo Avatar asked Oct 26 '16 15:10

Remi Avan-Nomayo


People also ask

What does \b do in grep?

3.3 The Backslash Character and Special Expressions The ' \ ' character, when followed by certain ordinary characters, takes a special meaning: ' \b ' Match the empty string at the edge of a word.

Can I use regex with grep?

The grep command (short for Global Regular Expressions Print) is a powerful text processing tool for searching through files and directories. When grep is combined with regex (regular expressions), advanced searching and output filtering become simple.

Can you use wildcards with grep?

The wildcard * (asterisk) can be a substitute for any number of letters, numbers, or characters. Note that the asterisk (*) works differently in grep. In grep the asterisk only matches multiples of the preceding character. The wildcard * can be a substitute for any number of letters, numbers, or characters.

How do you grep 2 patterns at a time?

The basic grep syntax when searching multiple patterns in a file includes using the grep command followed by strings and the name of the file or its path. The patterns need to be enclosed using single quotes and separated by the pipe symbol. Use the backslash before pipe | for regular expressions.


1 Answers

You are proabably using grep \bword\b which is really grep bwordb after bash parses the backslashes.

Use grep '\bword\b' (note the single-quotes).

like image 60
SIGSTACKFAULT Avatar answered Sep 22 '22 16:09

SIGSTACKFAULT