我想提取权利之间的值
<?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
假设 XML 文档格式良好(当前缺少五个结束标记),您可以使用它来检索文档中xmlstarlet
所有节点的值,如下所示:entitlements
$ xmlstarlet sel -t -v '//entitlements' -nl file.xml
600000001,600000080
600000300
要获取与特定 相关的值optionID
:
$ xmlstarlet sel -t -v '//assignedOption[optionID=1000929]/entitlements' -nl file.xml
600000300
答案2
缺乏 的优雅xmlstarlet
,但这种perl
方法返回您需要的输出
$ perl -ne 'while(m/<entitlements>(.*?)<\/entitlements>/g){print $1," "}' file.xml
600000001,600000080 600000300
awk
方法
$ awk -F\< '{for(a=1;a++<NF;){if($a~/^entitlements>/){print substr($a,14)}}}' file.xml
600000001,600000080
600000300
grep
方法
$ grep -o '<entitlements>[0-9,]*' file.xml|grep -o '[0-9,]*'
600000001,600000080
600000300