如果字符串中有点,则如何grep查找确切的单词

Paw*_*wan 10 linux bash

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

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

grep -l  "BML.I" *
Run Code Online (Sandbox Code Playgroud)

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

是否可以grep完全匹配 BML.I

Bri*_*new 18

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

grep -w -l "BML\.I" *
Run Code Online (Sandbox Code Playgroud)

注意上面有两个级别的转义.引号确保shell传递BML\.I给grep.在\随后逃逸期间grep.如果省略引号,那么shell会将其解释\为句点的转义(并且只是将未转义的句点传递给grep)


Ken*_*ent 7

尝试 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.)
Run Code Online (Sandbox Code Playgroud)