如何grep确切的文字字符串(没有正则表达式)

Nat*_*.B. 36 unix linux shell command-line grep

有没有办法grep(或使用另一个命令)找到确切的字符串,使用NO正则表达式?

例如,如果我想搜索(字面意思):

/some/file"that/has'lots\of"invalid"chars/and.triggers$(#2)[*~.old][3].html
Run Code Online (Sandbox Code Playgroud)

我不想通过逃避每一个"逃避".从本质上讲,我想通过它,就像我一样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
Run Code Online (Sandbox Code Playgroud)

小智 51

使用fgrep,它与grep -F(匹配固定字符串)相同.

  • `grep -F`现在似乎是首选."不推荐直接调用egrep或fgrep"[fgrep(1) - Linux手册页](http://linux.die.net/man/1/fgrep) (14认同)

Rub*_*ens 14

好吧,你可以把你想要匹配的信息放在一行,然后使用grep:

grep -F -f patterns.txt file.txt
Run Code Online (Sandbox Code Playgroud)

注意标志的使用-F,这导致grep将文件patterns.txt的每一行视为fixed-string要在file.txt中搜索.