我有一个xml文件emp.xml
<?xml>
<employee name="abc">ABC</employee>
<employee name="def">DEF</employee>
<!-- abc@gmail.com
this comment is added
to <employee name="gh">
</employee>
-->
?> 我想从这个文件中删除一些员工的详细信息。要删除的员工详细信息在其他文件delemp.txt abc gh中提到
while read line
do
i=\"$line\"
echo $i
find . -name "emp.xml" -type f | xargs sed -i -e '/employee name='$i'/,/<\/employee>/d'
done < delemp.txt我正在读取delemp.txt并获取要删除的员工详细信息,并搜索员工name=和员工结束标记,但此代码不起作用
即使我试着
sed -i '/employee name=\"abc\"/,/<\/employee>/d' emp.xml它正在删除前2行,即name="abc“和"def”
发布于 2019-04-04 01:15:54
将xml更正为有效
<?xml version="1.0" encoding="UTF-8"?>
<root>
<employee name="abc">ABC</employee>
<employee name="def">DEF</employee>
<!-- abc@gmail.com
this comment is added
to <employee name="gh">
</employee>
-->
</root>使用xmlstarlet和适当的xpath表达式删除XML节点
xmlstarlet ed -L -d '/root/employee[@name="abc"]' file.xml结果
<?xml version=xmlstarlet ed -L -d '/root/employee[@name="abc"]' file.xml"1.0" encoding="UTF-8"?>
<root>
<employee name="def">DEF</employee>
<!-- abc@gmail.com
this comment is added
to <employee name="gh">
</employee>
-->
</root>最后:
while read name; do
xmlstarlet ed -L -d "/root/employee[@name='$name']" file.xml
done < names.txt发布于 2019-04-04 01:24:24
在命令行中使用perl:
while read name; do
xml_grep2 -v "/root/emwantedployee[@name='$name']" file.xml > /tmp/new_file.xml &&
mv /tmp/new_file.xml file.xml
done < names.txt使用App::Xml_grep2 (待安装)
https://stackoverflow.com/questions/55500658
复制相似问题