Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash - xmlstarlet -v to $variable

wget ${RSS_URL} -O - 2>/dev/null | xmlstarlet sel -t -m "/rss/channel/item" -v "title" -n -v "link" -n -n

That line prints out the title (-v "title) and the url (-v "link") of entries in a rss feed I have. I want to store the title and url into separate variables ($title and $url for example) so I can use them for curl later.

How can I do this?

like image 581
qwerty1911 Avatar asked Jun 27 '26 02:06

qwerty1911


1 Answers

Run xmlstarlet twice, once for each information you want to get.

To make this efficient, you should download the URL to a local file (so only once wget) and then run xmlstarlet on that file:

title=$(xmlstarlet sel -t -m "/rss/channel/item" -v "title" local.xml)
link=$(xmlstarlet sel -t -m "/rss/channel/item" -v "link" local.xml)

I suggest this approach because it will play well with whitespace in the elements and even new lines.

Note: To optimize the process, you can first filter the XML input to produce a new XML file which just contains the information you want and then run xmlstarlet again on that to split it into variables.

like image 186
Aaron Digulla Avatar answered Jun 28 '26 17:06

Aaron Digulla



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!