yaka
yaka

Reputation: 137

Sed and variable containaing XML

I'm trying to pass a variable to sed that contains XML.

From this XML I would like to extract the parts contained between <image_viewer> </image_viewer> ; and <image_thumb_url> </image_thumb_url>

There is an HTML link between each of those tags, which has no spaces; and each tags + link are on a single line.

It seems I'm doining it completely wrong, I'm very new to bash...

base64 -w0 "$1" > "$1".64.jpg

apiXml=$(curl -v -F "upload=<$1.64.jpg" -F key="$apiKey" -F format=xml "$apiUrl")

imgView=$(echo "$apiXml" | sed 's/<image_viewer>\(.*\)<\/image_viewer>/\1/')
imgThumb=$(echo "$apiXml" | sed 's/<image_thumb_url>\(.*\)<\/image_thumb_url>/\1/')

imgLink="[url=$imgView][img]$imgThumb[/img][/url]"

echo "$imgLink"

Any suggestions would be welcome, thanks !

Upvotes: 1

Views: 109

Answers (1)

Charles Duffy
Charles Duffy

Reputation: 295413

It's easier to just use the right tool for the job here.

read imgView < <(xmlstarlet sel -t -m '//image_viewer' -v . -n <<<"$apiXml")
read imgThumb < <(xmlstarlet sel -t -m '//image_thumb_url' -v . -n <<<"$apiXml")

Any XPath engine will do -- XMLStarlet is just one of many.

Upvotes: 2

Related Questions