Ziv M
Ziv M

Reputation: 417

extract substring between charachters in specific posittion

I got string as followed:

text=\"abcdef\"gfijk\"lmno\"

How can I extract the text between last two " - so I will get only lmno ?

I tried to use & but without success

sub_text=${text&\"*}
echo ${sub_text&\"*}

Upvotes: 0

Views: 13

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295698

This is easily done with parameter expansion.

First, delete that final quote with ${var%pattern}, which removes the shortest match for pattern from the end of $var:

result=${text%'"'}     # result=\"abcdef\"gfijk\"lmno

Then, delete everything from the beginning up to the last remaining quote with ${var##pattern}, which removes the longest match for pattern from the beginning of $var:

result=${result##*'"'} # result=lmno

...and then you're there:

echo "$result"

Upvotes: 2

Related Questions