我从 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 -> Alerts & Logging -> SNMP)</systemContact><systemDescription>Linux xxxxx 3.10.0-uc0 #1 Mon Dec 21 16:21:01 EST 2015 armv4tl</systemDescription><systemLocation>Unspecified (Management Console -> Alerts & Logging -> 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>
如何检索 <id>
之间的文本和 </id>
?我尝试使用:
sed -e 's/<id>\(.*\)<\/id>/\1/'
但这会导致打印出整个文本 block ,而不仅仅是 <\id> 和 <\id> 之间的部分。
我没有能力在这个盒子上安装程序,所以我宁愿不经历手动编译 xmlstarlet 等的麻烦,只是为了提取这个东西。
最佳答案
感谢您解释为什么您没有使用正确的工具做显而易见的事情
sed -e 's/.*<id>\(.*\)<\/id>.*/\1/'
也匹配你不想要的(未经测试)
关于xml - 如何使用 sed 从两个 xml 标记之间提取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37954065/