45

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 Answers 2

71

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

2
  • Worked perfectly with fgrep 'preg_replace("/([a-z]+)([A-Z])/"' . -R (which failed with grep). You will still have to escape $, but that's livable. Aug 7, 2013 at 18:49
  • 19
    grep -F seems to be preferred now. "Direct invocation as either egrep or fgrep is deprecated" fgrep(1) - Linux man page Jan 6, 2015 at 17:39
20

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.

1
  • 1
    It's worth noting that this is the same as fgrep -f patterns.txt file.txt. By using a file of patterns, $ does not need to be escaped. I ultimately decided to accept the other because it's the least time consuming. Aug 7, 2013 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.