<分区>
有什么方法可以让 grep 匹配精确字符串,而不是将其解析为正则表达式?或者是否有一些工具可以为 grep 正确转义字符串?
$ version=10.4
$ echo "10.4" | grep $version
10.4
$ echo "1034" | grep $version # shouldn't match
1034
<分区>
有什么方法可以让 grep 匹配精确字符串,而不是将其解析为正则表达式?或者是否有一些工具可以为 grep 正确转义字符串?
$ version=10.4
$ echo "10.4" | grep $version
10.4
$ echo "1034" | grep $version # shouldn't match
1034
最佳答案
使用 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 string 和 How can grep interpret literally a string that contains an asterisk and is fed to grep through a variable?)
关于bash - grep 匹配完全子字符串忽略正则表达式语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6621795/