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,541
  • 7
  • 31
  • 38

4 Answers4

20

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 Quénot
  • 173,512
  • 41
  • 224
  • 223
9
$ cat aaaa
foo"bar"base

$ grep -o '"[^"]\+"'
"bar"
Fredrik Pihl
  • 44,604
  • 7
  • 83
  • 130
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,748
  • 1
  • 16
  • 33
  • 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
1

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'