13

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

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202
Pawan
  • 31,545
  • 102
  • 256
  • 434

4 Answers4

21

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)

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
10

try grep -wF

from man page:

 -w, --word-regexp
                  Select  only  those  lines containing matches that form whole words.  The
                  test is that the matching substring must either be at  the  beginning  of
                  the line, or preceded by a non-word constituent character.  Similarly, it
                  must be either at  the  end  of  the  line  or  followed  by  a  non-word
                  constituent  character.  Word-constituent characters are letters, digits,
                  and the underscore.



 -F, --fixed-strings
              Interpret  PATTERN as a list of fixed strings, separated by newlines, any
              of which is to be matched.  (-F is specified by POSIX.)
Kent
  • 189,393
  • 32
  • 233
  • 301
4

I use fgrep, which is the same as grep -F

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
0

Use this command:

ls | grep -x "BML.I"

Krzysztof Atłasik
  • 21,985
  • 6
  • 54
  • 76