【发布时间】:2021-12-11 18:02:41
【问题描述】:
我正在尝试从以下 xml 标记中提取和连接 <id> 和 <name> 的值
<applications><application>
<id>292</id>
<name>Test</name>
<accountGuid>2-b01e-2ef14d11c5e0</accountGuid>
</application>
<application>
<id>195</id>
<name>State</name>
<accountGuid>2-b01e-2ef14d11c5e0</accountGuid>
</application>
<application>
<id>266</id>
<name>Home</name>
<accountGuid>2-b01e-2ef14d11c5e0</accountGuid>
</application>
</applications>
想要的输出是
292:Test
195:state
266:Home
我使用下面的 sed 提取,但它给了我输出
sed -n 's:.*<id>\(.*\)</id>.*:\1:p;s:.*<name>\(.*\)</name>.*:\1:p'
292
Test
195
state
266
Home
请大家帮忙看看我应该怎么做才能获得所需的输出?
【问题讨论】:
-
Don't Parse XML/HTML With Regex. 我建议使用 XML/HTML 解析器 (xmlstarlet, xmllint ...)。