Escape double quote in grep

LinuxShell

Linux Problem Overview


I wanted to do grep for keywords with double quotes inside. To give a simple example:

echo "member":"time" | grep -e "member\""

That does not match. How can I fix it?

Linux Solutions


Solution 1 - Linux

The problem is that you aren't correctly escaping the input string, try:

echo "\"member\":\"time\"" | grep -e "member\""

Alternatively, you can use unescaped double quotes within single quotes:

echo '"member":"time"' | grep -e 'member"'

It's a matter of preference which you find clearer, although the second approach prevents you from nesting your command within another set of single quotes (e.g. ssh 'cmd').

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionQiang LiView Question on Stackoverflow
Solution 1 - LinuxcmhView Answer on Stackoverflow