grep匹配精确的子串忽略正则表达式语法

时间:2011-07-08 08:19:38

标签: bash grep

有没有办法让grep匹配完全字符串,而不是将其解析为正则表达式?或者是否有一些工具可以正确地为grep转义字符串?

$ version=10.4
$ echo "10.4" | grep $version
10.4
$ echo "1034" | grep $version # shouldn't match
1034

1 个答案:

答案 0 :(得分:14)

使用grep -F或fgrep。

$ echo "1034" | grep -F $version # shouldn't match
$ echo "10.4" | grep -F $version
10.4

参见手册页:

   -F, --fixed-strings
         Interpret PATTERN as a list of fixed strings, separated
         by newlines, any of which is to be matched.

我一直在寻找术语“文字匹配”或“固定字符串”。

(另请参阅Using grep with a complex stringHow can grep interpret literally a string that contains an asterisk and is fed to grep through a variable?