gpt4 book ai didi

xml - 将 XML 标记的内容复制到一组新的 XML 标记中

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:06:50 28 4
gpt4 key购买 nike

我正在尝试仅使用 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>

非常感谢任何帮助。

最佳答案

试试这个:

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'

关于xml - 将 XML 标记的内容复制到一组新的 XML 标记中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12207580/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com