Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape double quote in grep

Tags:

linux

shell

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?

like image 306
Qiang Li Avatar asked Aug 16 '12 21:08

Qiang Li


People also ask

How do you escape a double quote in grep?

Escaping. Generally, the backslash (\) character is yet another option to quash the inbuilt shell interpretation and tell the shell to accept the symbol literally.

How do you escape a double quote in Linux?

' appearing in double quotes is escaped using a backslash. The backslash preceding the ' !

How do you escape characters in grep?

If you include special characters in patterns typed on the command line, escape them by enclosing them in single quotation marks to prevent inadvertent misinterpretation by the shell or command interpreter. To match a character that is special to grep –E, put a backslash ( \ ) in front of the character.

How do you escape single quotes in grep?

You cannot escape single quotes that appear within single quotes. As explained in the bash manual: Enclosing characters in single quotes (''') preserves the literal value of each character within the quotes. A single quote may not occur between single quotes, even when preceded by a backslash.


1 Answers

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').

like image 52
cmh Avatar answered Sep 25 '22 02:09

cmh