【发布时间】: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>
非常感谢任何帮助。
【问题讨论】:
-
以这种方式使用
sed是一个非常糟糕的主意。不如尝试使用正则表达式解析 HTML 糟糕,但仍然很糟糕。查看此问题的第二个答案:stackoverflow.com/questions/893585/how-to-parse-xml-in-bash