使用shell脚本中的xmllint从XML文件中删除一行

时间:2015-05-20 13:09:53

标签: xml bash shell xmllint

我在特定目录中有一个xml文件现在我想从文件中删除一个特定的条目(参见示例),并使用shell脚本更新现有的xml文件。

示例:(参见代码)我必须删除第4行(特定资源的条目)

<?xml version="1.0" encoding="UTF-8"?>
<manifest:manifest xmlns:manifest="....." manifest:version="1.2">
 <manifest:file-entry manifest:full-path="/" manifest:version="1.2" manifest:media-type="..."/>
"line to be deleted" <manifest:file-entry manifest:full-path="...." manifest:media-type="..."/>
 ......some more lines....
 </manifest:manifest>

帮助将不胜感激:)
我是新手,对编辑xml文件不太了解,从某些来源我发现可以使用xmllint,但我不确定。

1 个答案:

答案 0 :(得分:1)

据我所知,xmllint并非用于编辑XML文件,因此您无法做到这一点。

您的问题的一个可能解决方案是命令:

grep -v your_pattern your_file > new_file

它将选择除一个之外的所有其他行并将其复制到新文件中。

另一种可能更好的方法是使用xmlstarletdocumentation)。

你可以像这样使用它:

xmlstarlet ed --delete your_xpath input.xml > output.xml