使用以下输入:
$ cat file
<node1>
bla
<remove>
abc
</remove>
kkk
</node1>
<node9>
bla
<remove>
abc
</remove>
kkk
</node9>
以下脚本将使用删除所需的标签GNU awk
:
$ awk '/<node1>/{gsub(/<[/]?remove>/," ")}
{printf "%s%s",$0,RT}' RS='</node[0-9]+>' file | grep '\S'
<node1>
bla
abc
kkk
</node1>
<node9>
bla
<remove>
abc
</remove>
kkk
</node9>
如果在一行中找不到标签,该脚本甚至可以完成这项工作:
$ cat file
<node1>bla<remove>abc</remove>kkk</node1>
<node9>bla<remove>abc</remove>kkk</node9>
$ awk '/<node1>/{gsub(/<[/]?remove>/," ")}
{printf "%s%s",$0,RT}' RS='</node[0-9]+>' file
<node1>bla abc kkk</node1>
<node9>bla<remove>abc</remove>kkk</node9>