bash grep 匹配精确的子字符串忽略正则表达式语法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6621795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
grep match exact substring ignoring regex syntax
提问by johv
Is there some way to make grep match an exactstring, and not parse it as a regex? Or is there some tool to escape a string properly for grep?
有没有办法让 grep 匹配一个精确的字符串,而不是将它解析为正则表达式?或者是否有一些工具可以为grep正确转义字符串?
$ version=10.4
$ echo "10.4" | grep $version
10.4
$ echo "1034" | grep $version # shouldn't match
1034
回答by johv
Use grep -F or fgrep.
使用 grep -F 或 fgrep。
$ echo "1034" | grep -F $version # shouldn't match
$ echo "10.4" | grep -F $version
10.4
See man page:
请参阅手册页:
-F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.
-F, --fixed-strings Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.
I was looking for the term "literal match" or "fixed string".
我正在寻找术语“文字匹配”或“固定字符串”。
(See also Using grep with a complex stringand How can grep interpret literally a string that contains an asterisk and is fed to grep through a variable?)

