解析curl输出命令行

时间:2017-08-23 09:11:07

标签: xml bash curl

响应一个curl命令KassenzeichenString我得到了一个巨大的xml,其中有许多类似于以下内容的

curl -ik -u max:pass -X GET -i -H "Content-Type: application/xml" https://1.2.3.4:943/cloud/rest/projects

如何解析此输出以获取基于<project xlink:href="https://1.2.3.4:943/cloud/projects/13389/" > <approvedservers xlink:href="https://1.2.3.4:943/cloud/projects/13389/approvedservers/" ></approvedservers> <requestUser>maxadmin</requestUser> <removed xlink:href="https://1.2.3.4:943/cloud/projects/13389/removed/" ></removed> <rejected>false</rejected> <pending xlink:href="https://1.2.3.4:943/cloud/rest/projects/13389/pending/" ></pending> <name>4BSm3ZQ1T6q7xBR2xT2PdQ</name> <servers xlink:href="https://1.2.3.4:943/cloud/rest/projects/13389/servers/" ></servers> <ticketId>ticketId</ticketId> <approved>true</approved> <history xlink:href="https://1.2.3.4:943/cloud/rest/projects/13389/history/" ></history> <startTime>1502214154651</startTime> <requestedServerCount>1</requestedServerCount> <applicationid>4336525</applicationid> <provision>provision</provision> <customer>None</customer> <endTime>253402300740000</endTime> </project> 在所有网址https://1.2.3.4:943/cloud/rest/projects/13389/pending/中相同的项目ID。基本上我想找到用户名的项目ID。

1 个答案:

答案 0 :(得分:1)

您可以使用xmlstarlet并搜索所有具有所需文字作为值的name元素,然后输出同级元素pending(使用../pending/@href:xlink,这意味着“向上一个级别,然后是元素pending,然后是属性xlink:href):

xmlstarlet sel -t  --match '//name[text() = "4BSm3ZQ1T6q7xBR2xT2PdQ"]' -v "../pending/@xlink:href"   file.xml