Skip to content
Advertisement

How can I extract text from between two xml tags using sed

I have the following output from 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>

How can I retrieve the text between <id> and </id>? I tried using:

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

but that results in the entire block of text being printed out instead of just the part between and <id>.

I don’t have the ability to install programs on this box so I’d rather not go through the hassle of manually compiling xmlstarlet, etc, just to extract this one thing.

Advertisement

Answer

Thanks for explaining why you are not doing the obvious with the correct tools

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

match what you don’t want too (untested)

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement