将XML标记的内容复制到一组新的XML标记中
我正在尝试仅使用Linux CLI工具处理XML文件。 我试图解决的主要问题是将特定XML标记的内容复制到新标记中,如下所示:将XML标记的内容复制到一组新的XML标记中,xml,linux,sed,Xml,Linux,Sed,我正在尝试仅使用Linux CLI工具处理XML文件。 我试图解决的主要问题是将特定XML标记的内容复制到新标记中,如下所示: <date>Wednesday</date> <name>The Name</name> <anotherattribute>Attribute</anotherattribute> 星期三 名字 属性 进入: 星期三 名字 名字 属性 我一直在尝试使用sed来解决此问题,并且能够识别标记,并
<date>Wednesday</date>
<name>The Name</name>
<anotherattribute>Attribute</anotherattribute>
星期三
名字
属性
进入:
星期三
名字
名字
属性
我一直在尝试使用sed来解决此问题,并且能够识别标记,并将其复制到保留缓冲区:
/<name>/{
h
i\
<id>
G
a\
</id>
}
//{
H
我\
G
a\
}
但结果是:
<date>Wednesday</date>
<id>
<name>The Name</name>
<name>The Name</name>
</id>
<anotherattribute>Attribute</anotherattribute>
星期三
名字
名字
属性
非常感谢您的帮助。试试以下方法:
sed '/<name>/{h;s/name>/id>/g;G}'
试试这个:
sed '/<name>/{h;s/name>/id>/g;G}'
这样使用
sed
是一个非常糟糕的主意。没有试图用正则表达式解析HTML那么糟糕,但仍然很糟糕。查看这个问题的第二个答案:以这种方式使用sed
是一个非常糟糕的主意。没有试图用正则表达式解析HTML那么糟糕,但仍然很糟糕。查看此问题的第二个答案:
cat input.xml |
xmlstarlet ed -i //name -t elem -n id -v '' |
xmlstarlet ed -u //id -x '../name'