如果string已经有点,那么如何查找确切的单词

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

当我尝试使用下面的命令:

 grep -l "BML.I" * 

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

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

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

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

注意在上面有两个级别的转义。 引号确保shell将BML\.I传递给grep。 \然后逃脱grep的期限。 如果您省略了引号,那么shell会将该\解释为这段时间的转义(并且会简单地将未转义的句点传递给grep

试试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.) 

我使用fgrep ,它与grep -F相同