如果字符串中包含点,则如何grep输入确切的单词

问题描述:

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

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" *

如果包含单词BML

是否可以对完全匹配的BML.I

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

您需要对进行转义. (句号),因为默认情况下它会与任何字符匹配,并指定-w来匹配特定的单词,例如

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" *

请注意,上面有两个转义级别.引号确保外壳程序将BML\.I传递给grep.然后\转义grep的句点.如果省略引号,则外壳程序会将\解释为该时间段的转义符(并将简单地将未转义的时间段传递给grep)

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)