【问题标题】:Copy contents of XML tag into a new set of XML tags将 XML 标记的内容复制到一组新的 XML 标记中
【发布时间】:2012-08-31 00:52:34
【问题描述】:

我正在尝试仅使用 Linux CLI 工具处理 XML 文件。 我要解决的主要问题是将特定 XML 标记的内容复制到新标记中,如下所示:

<date>Wednesday</date>
<name>The Name</name>
<anotherattribute>Attribute</anotherattribute>

进入:

<date>Wednesday</date>
<id>The Name</id>
<name>The Name</name>
<anotherattribute>Attribute</anotherattribute>

我一直在尝试使用sed来解决这个问题,并且已经能够识别标签,并将其复制到保持缓冲区中:

/<name>/{
h
i\
<id>
G
a\
</id>
}

但这会导致:

<date>Wednesday</date>
<id>
<name>The Name</name>
<name>The Name</name>
</id>
<anotherattribute>Attribute</anotherattribute>

非常感谢任何帮助。

【问题讨论】:

标签: xml linux sed


【解决方案1】:

试试这个:

sed '/<name>/{h;s/name>/id>/g;G}'

你也可以试试xmlstarlet:

cat input.xml |
    xmlstarlet ed -i //name -t elem -n id -v '' |
        xmlstarlet ed -u //id -x '../name'

【讨论】:

    猜你喜欢
    • 2021-01-13
    • 1970-01-01
    • 2010-09-06
    • 1970-01-01
    • 2017-02-25
    • 1970-01-01
    • 2016-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多