【问题标题】:Use sed to concatenate xml tags [duplicate]使用 sed 连接 xml 标签 [重复]
【发布时间】: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:.*&lt;id&gt;\(.*\)&lt;/id&gt;.*:\1:p;s:.*&lt;name&gt;\(.*\)&lt;/name&gt;.*:\1:p'

292
Test
195
state
266
Home

请大家帮忙看看我应该怎么做才能获得所需的输出?

【问题讨论】:

标签: xml bash unix sed


【解决方案1】:

使用 xmlstarlet:

xmlstarlet select --text --template --match "/applications/application" --value-of "concat(id,':',name)" -n file.xml

输出:

292:测试 195:状态 266:首页

见:xmlstarlet select --help

【讨论】:

    猜你喜欢
    • 2017-12-12
    • 2016-11-29
    • 1970-01-01
    • 2015-10-07
    • 1970-01-01
    • 2015-02-07
    • 2015-09-06
    • 2023-04-08
    • 1970-01-01
    相关资源
    最近更新 更多