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
使用这个命令:
ls | grep -x "BML.I"