Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to grep for the exact word if the string has got dot in it

Tags:

linux

bash

I was trying to search for a particular word BML.I in a current directory.

When I tried with the below command:

grep -l  "BML.I" *

It is displaying all the results if it contains the word BML

Is it possible to grep for the exact match BML.I

like image 811
Pawan Avatar asked Mar 20 '13 11:03

Pawan


1 Answers

You need to escape the . (period) since by default it matches against any character, and specify -w to match a specific word e.g.

grep -w -l "BML\.I" *

Note there are two levels of escaping in the above. The quotes ensure that the shell passes BML\.I to grep. The \ then escapes the period for grep. If you omit the quotes, then the shell interprets the \ as an escape for the period (and would simply pass the unescaped period to grep)

like image 85
Brian Agnew Avatar answered Sep 17 '22 13:09

Brian Agnew