Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grep string inside double quotes

Tags:

linux

grep

bash

Trying to grep a string inside double quotes at the moment I use this

grep user file | grep -e "[\'\"]"

This will get to the section of the file I need and highlight the double quotes but it will not give the sting in the double quotes

like image 579
Grimlockz Avatar asked Mar 15 '13 14:03

Grimlockz


People also ask

How do you escape a double quote in grep?

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

Do you use quotes with grep?

Whenever you use a grep regular expression at the command prompt, surround it with quotes, or escape metacharacters (such as & ! . * $ ? and \ ) with a backslash ( \ ).

How do you grep inverted commas?

grep will show a matching line, so all you have to do is to find the two double-quotes... If you want to extract a word that is within double quotes you can, for example, do... Or use grep -o to only show the matching portion of the line (with a non-greedy regex).

How do you escape special characters in grep command?

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.


1 Answers

Try doing this :

$ cat aaaa
foo"bar"base
$ grep -oP '"\K[^"\047]+(?=["\047])' aaaa
bar

I use look around advanced regex techniques.

If you want the quotes too :

$ grep -Eo '["\047].*["\047]'
"bar"

Note :

\047

is the octal ascii representation of the single quote

like image 107
Gilles Quenot Avatar answered Sep 20 '22 09:09

Gilles Quenot