我有一个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并删除要删除的员工详细信息,并搜索员工姓名=和员工结束标签,但是此代码不起作用
即使我尝试
sed -i '/employee name=\"abc\"/,/<\/employee>/d' emp.xml
它将删除前两行,即name =“ abc”和“ def”
答案 0 :(得分:0)
<?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 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
答案 1 :(得分:0)
在命令行中使用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(要安装)