Linux 如何 grep 精确的文字字符串(无正则表达式)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14347722/
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
How to grep exact literal string (no regex)
提问by Nathan J.B.
Is there a way to grep
(or use another command) to find exact strings, using NO regex?
有没有办法grep
(或使用另一个命令)不使用正则表达式来查找确切的字符串?
For example, if I want to search for (literally):
例如,如果我想搜索(字面意思):
/some/file"that/has'lots\of"invalid"chars/and.triggers$(#2)[*~.old][3].html
I don't want to go through and escape every single "escapable". Essentially, I want to pass it through, like I would with echo
:
我不想经历和逃避每一个“逃避”。本质上,我想通过它,就像我一样echo
:
$ echo "/some/file\"that/has'lots\of\"invalid\"chars/and.triggers$(#2)[*~.old][3].html"
/some/file"that/has'lots\of"invalid"chars/and.triggers$(#2)[*~.old][3].html
采纳答案by pldoverflow
Use fgrep
, it's the same as grep -F
(matches a fixed string).
使用fgrep
,它与grep -F
(匹配固定字符串)相同。
回答by Rubens
Well, you can put the information you want to match, each in a line, and then use grep
:
好吧,你可以把你想要匹配的信息,每一个都放在一行,然后使用grep
:
grep -F -f patterns.txt file.txt
Notice the usage of the flag -F
, which causes grep
to consider each line of the file patterns.txtas a fixed-string
to be searched in file.txt.
注意标志的用法-F
,它导致grep
将文件patterns.txt 的每一行视为fixed-string
要在file.txt 中搜索的a 。