13

BML.I我试图在当前目录中搜索特定单词。

当我尝试使用以下命令时:

grep -l  "BML.I" *

如果它包含单词,它将显示所有结果BML

是否可以 grep 精确匹配BML.I

4

4 回答 4

21

你需要逃避 . (句点)因为默认情况下它匹配任何字符,并指定 -w 以匹配特定单词,例如

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

请注意,上面有两个级别的转义。引号确保 shell 传递BML\.I给 grep。然后\逃过grep. 如果省略引号,则 shell 将 解释\为句点的转义(并且只需将未转义的句点传递给grep

于 2013-03-20T11:02:14.277 回答
10

尝试grep -wF

从手册页:

 -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.)
于 2013-03-20T11:05:12.790 回答
4

我使用fgrep,这与grep -F

于 2013-03-20T11:23:43.360 回答
0

使用这个命令:

ls | grep -x "BML.I"

于 2019-07-22T06:36:23.120 回答