11

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

Grimlockz
  • 2,381
  • 7
  • 28
  • 37

4 Answers4

19

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

Gilles Quenot
  • 154,891
  • 35
  • 213
  • 206
8
$ cat aaaa
foo"bar"base

$ grep -o '"[^"]\+"'
"bar"
Fredrik Pihl
  • 42,950
  • 7
  • 81
  • 128
3

Try:

grep "Test" /tmp/junk | tr '"' ' '

This will remove quotes from the output of grep

Or you could try the following:

grep "Test" /tmp/junk | cut -d '"' -f 2

This will use the quotes as a delimiter. Just specify the field you want to select. This lets you cherry-pick the information you want.

Paul Calabro
  • 1,578
  • 1
  • 15
  • 32
  • 1
    Ironically, both of these yield a blank line when piped onto `grep -o '"[^"]\+"'`, but `grep -Eo '".*"' | tr -d '"'` worked. – Abandoned Cart Jul 09 '14 at 20:36
  • `cut` is not a good choice here because the information is not separated into fields. The number of quotes in a line could be 1, could be 5, could be none. – SaltyNuts Dec 03 '15 at 17:38
0

This worked for me. This will print the data including the double quotes also.

 grep -o '"[^"]\+"' file.txt

If we want to print without double quotes, then we need to pipe the grep output to a sed command.

grep -o '"[^"]\+"' file.txt | sed 's/"//g'