1

I wanted to extract the values between entitlement

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><HorizonResponse><assignedDevices><assignedDevice><deviceType>EOS</deviceType><assignedProduct><installDate>2019-09-19T00:00:00+02:00</installDate><entitlements>600000001,600000080</entitlements><serviceIDs></serviceIDs>2019-09-19T00:00:00+02:00</installDate><deInstallDate>1753-01-01T00:00:00+01:00</deInstallDate><casIndicator>14</optionQuantity><entitlements>600000300</entitlements><serviceIDs><assignedOption>
      ...
1
  • Please put clarifications in the actual question, not in comments. Also, your XML seems to be partial as there are several end-tags missing.
    – Kusalananda
    Dec 6 '19 at 10:02
1

Assuming that the XML document is well formed (it currently lacks five closing tags), you may use xmlstarlet to retrieve the values for all entitlements nodes in the document like so:

$ xmlstarlet sel -t -v '//entitlements' -nl file.xml
600000001,600000080
600000300

To get the value related to a particular optionID:

$ xmlstarlet sel -t -v '//assignedOption[optionID=1000929]/entitlements' -nl file.xml
600000300
1

Lacks the elegance of xmlstarlet, but this perl approach returns the output you require

$ perl -ne 'while(m/<entitlements>(.*?)<\/entitlements>/g){print $1," "}' file.xml
600000001,600000080 600000300

awk approach

$ awk -F\< '{for(a=1;a++<NF;){if($a~/^entitlements>/){print substr($a,14)}}}' file.xml
600000001,600000080
600000300

grep approach

$ grep -o '<entitlements>[0-9,]*' file.xml|grep -o '[0-9,]*'
600000001,600000080
600000300

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.