【问题标题】:How can I extract text from between two xml tags using sed如何使用 sed 从两个 xml 标签之间提取文本
【发布时间】:2016-10-23 13:42:19
【问题描述】:

我从 curl 得到以下输出:

<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'><env:Header></env:Header><env:Body><ns3:getNodesResponse xmlns:ns3='http://node.sdk.nms.ov.hp.com/'><return><item><created>2016-01-12T18:04:44.617Z</created><deviceCategory>com.hp.ov.nms.devices.other</deviceCategory><deviceDescription>No Device Profile found for this device. The assigned SNMP OID is the top of a Opto 22 subtree</deviceDescription><deviceFamily>com.hp.ov.nms.devices.opengear</deviceFamily><deviceModel>Opengear Generic</deviceModel><deviceVendor>com.hp.ov.nms.devices.opengear</deviceVendor><discoveryState>DISCOVERY_COMPLETED</discoveryState><endNode>false</endNode><>false</IPv4Router><id>330742910324</id><lanSwitch>false</><longName>xxxxxxxxxxxxxxxxx</longName><managementMode>MANAGED</managementMode><modified>2016-06-21T19:46:38.837Z</modified><name>xxxxxxxxxxxxxxx</name><notes></notes><snmpSupported>true</snmpSupported><snmpVersion>V2C</snmpVersion><status>NORMAL</status><systemContact>Unspecified (Management Console -&gt; Alerts &amp; Logging -&gt; SNMP)</systemContact><systemDescription>Linux xxxxx 3.10.0-uc0 #1 Mon Dec 21 16:21:01 EST 2015 armv4tl</systemDescription><systemLocation>Unspecified (Management Console -&gt; Alerts &amp; Logging -&gt; SNMP)</systemLocation><systemName>xxxxxx</systemName><systemObjectId>.1.3.6.1.4.1.25049.1.81</systemObjectId><uuid>e3a77c1c-ab82-4dfb-b7bf-78e1dadef888</uuid></item></return></ns3:getNodesResponse></env:Body></env:Envelope>

如何检索&lt;id&gt;&lt;/id&gt; 之间的文本?我尝试使用:

sed -e 's/<id>\(.*\)<\/id>/\1/'

但这会导致整个文本块被打印出来,而不仅仅是和 之间的部分。

我没有能力在这个盒子上安装程序,所以我宁愿不去手动编译xmlstarlet等的麻烦,只是为了提取这个东西。

【问题讨论】:

    标签: xml linux sed


    【解决方案1】:

    感谢您解释为什么您没有使用正确的工具做显而易见的事情

    sed -e 's/.*<id>\(.*\)<\/id>.*/\1/'
    

    匹配你不想要的东西(未经测试)

    【讨论】:

    • 谢谢!我最初一直在使用一个 python suds 库,它为此目的工作得很好,但不幸的是我无法让它在这台特定的机器上正常工作
    猜你喜欢
    • 2014-11-13
    • 1970-01-01
    • 2016-05-10
    • 2012-10-25
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 2013-03-18
    • 1970-01-01
    相关资源
    最近更新 更多