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
Generally, the backslash (\) character is yet another option to quash the inbuilt shell interpretation and tell the shell to accept the symbol literally.
Whenever you use a grep regular expression at the command prompt, surround it with quotes, or escape metacharacters (such as & ! . * $ ? and \ ) with a backslash ( \ ).
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).
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With