Grimlockz
Grimlockz

Reputation: 2581

Grep string inside double quotes

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

Upvotes: 11

Views: 42633

Answers (4)

Alan Saldanha
Alan Saldanha

Reputation: 23

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'

Upvotes: 1

Gilles Quénot
Gilles Quénot

Reputation: 185053

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

Upvotes: 20

Fredrik Pihl
Fredrik Pihl

Reputation: 45652

$ cat aaaa
foo"bar"base

$ grep -o '"[^"]\+"'
"bar"

Upvotes: 9

Paul Calabro
Paul Calabro

Reputation: 1906

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.

Upvotes: 3

Related Questions