How to grep exact literal string (no regex)

2020-02-24 07:18发布

Is there a way to grep (or use another command) to find exact strings, using NO regex?

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 "/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

2条回答
时光不老,我们不散
2楼-- · 2020-02-24 07:48

Use fgrep, it's the same as grep -F (matches a fixed string).

查看更多
女痞
3楼-- · 2020-02-24 07:48

Well, you can put the information you want to match, each in a line, and then use 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.txt as a fixed-string to be searched in file.txt.

查看更多
登录 后发表回答