Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match string between quotes

I'm using a shell script to read in a file and then piping the output to grep and trying to extract the string contained between two quotes (while excluding the quotes).

./readFile.sh | grep -e "[\^\"]*[\?\"]"

This returns the entire contents of the file I that I'm reading.

My file is organized this way:

TITLE="foo"
DATA="bar"
SERVER="foo.bar.server"

I read the regex tutorial here http://www.regular-expressions.info/lookaround.html and tried to use the lookahead and lookbehind as best as I could, but I don't understand what's wrong here.

like image 424
hax0r_n_code Avatar asked May 05 '26 05:05

hax0r_n_code


2 Answers

check this example with grep with look-behind

kent$  echo 'TITLE="foo"
DATA="bar"
SERVER="foo.bar.server"'|grep -Po '(?<=")[^"]*'
foo
bar
foo.bar.server

alternative is grep -Po '"\K[^"]*'

like image 197
Kent Avatar answered May 07 '26 08:05

Kent


If you want to give awk a chance it is pretty simple:

awk -F '"' 'NF>2{print $2}' inFile
like image 41
anubhava Avatar answered May 07 '26 08:05

anubhava



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!