[英]How to grep for the exact word if the string has got dot in it
我试图在当前目录中搜索特定单词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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.