Martin Mueller
Martin Mueller

Reputation: 25

find string including quotation marks using grep

How can I find a string which includes quotation marks with grep? I tried a backslash to escape but this doesn't work.

For example search for the string "localStorage['api']". I tried:

grep -r "localStorage['api']" /path
grep -r "localStorage[\'api\']" /path

Upvotes: 0

Views: 754

Answers (1)

fedorqui
fedorqui

Reputation: 289625

Your escaping is OK. The problem lies in the [], that grep understands as regular expressions. Thus, you need to somehow tell it to treat the string as literal. For this we have -F:

grep -F "localStorage['api']" file

Test

$ cat a
hello localStorage['api'] and blabla
bye
$ grep -F "localStorage['api']" a
hello localStorage['api'] and blabla

From man grep:

-F, --fixed-strings

Interpret PATTERN as a list of fixed strings, separated by newlines, any of which is to be matched.

Upvotes: 1

Related Questions