且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 bash 脚本添加/删除 xml 标签

更新时间:2023-12-05 18:51:22

这在 sed 中不难做到,因为 sed 也适用于范围.

This would not be difficult to do in sed, as sed also works on ranges.

试试这个(假设 xml 在一个名为 foo.xml 的文件中):

Try this (assuming xml is in a file named foo.xml):

sed -i '/<b>/,/</b>/d' foo.xml

-i 将更改写入原始文件(使用 -i.bak 保留原始文件的备份副本)

-i will write the change into the original file (use -i.bak to keep a backup copy of the original)

此 sed 命令将对范围指定的所有行执行操作 d(删除)

This sed command will perform an action d (delete) on all of the lines specified by the range

# all of the lines between a line that matches <b>
# and the next line that matches </b>, inclusive
/<b>/,/</b>/

所以,用简单的英语,这个命令将删除带有 <b> 的行之间和包括行之间的所有行.以及带有 </b>

So, in plain English, this command will delete all of the lines between and including the line with <b> and the line with </b>

如果您想注释掉这些行,请尝试以下方法之一:

If you'd rather comment out the lines, try one of these:

# block comment
sed -i 's/<b>/<!-- <b>/; s/</b>/</b> -->/' foo.xml

# comment out every line in the range
sed -i '/<b>/,/</b>/s/.*/<!-- & -->/' foo.xml