【发布时间】:2017-04-24 09:48:33
【问题描述】:
我有一个类似这样的文件
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="configuration.xsl"?>
<configuration>
<property><name>key1.somestuff.someotherstuff</name><value>value1</value></property><property><name>key2.somestuff.someotherstuff</name><value>value2</value></property></configuration>
所以我作为输入获得的文件格式不正确,我需要将其中一些与模式匹配的属性复制到另一个 xml 文件。 我如何在 shell 中为这个文件提取段(使用 grep 或 sed 或任何此类工具),以获取键中的给定模式。 例如,如果代码格式正确,我可以使用:
grep --no-group-separator -a2 "key1"
如何为某个键(如 key1)提取段:
<property>
<name>key1.somestuff.someotherstuff</name>
<value>value1</value>
</property>
【问题讨论】:
-
您的 xml 输入不是有效的 xml。
-
@Cyrus 是的,抱歉,这是手写示例,已更正。
-
好的。您的示例包含两次
key1.somestuff.someotherstuff。这是故意的吗? -
@Cyrus 只是为了给出格式,将名称更改为不同的值以具有不同的值。不,那不是故意的
-
@user1560339:你能用
xmllint吗?可以下载安装吗?