【发布时间】:2010-04-08 03:55:38
【问题描述】:
我有一个 sed 命令来注释掉 xml 命令
sed 's/^\([ \t]*\)\(.*[0-9a-zA-Z<].*\)$/\1<!-- Security: \2 -->/' web.xml
拍摄:
<a>
<!-- Comment -->
<b>
bla
</b>
</a>
生产:
<!-- Security: <a> -->
<!-- Security: <!-- Comment --> --> // NOTE: there are two end comments.
<!-- Security: <b> -->
<!-- Security: bla -->
<!-- Security: </b> -->
<!-- Security: </a> -->
理想情况下,我不想使用我的 sed 脚本来评论已评论的内容。
即:
<!-- Security: <a> -->
<!-- Comment -->
<!-- Security: <b> -->
<!-- Security: bla -->
<!-- Security: </b> -->
<!-- Security: </a> -->
我可以这样做:
sed 's/^\([ \t]*\)\(.*[0-9a-zA-Z<].*\)$/\1<!-- Security: \2 -->/' web.xml
sed 's/^[ \t]*<!-- Security: \(<!--.*-->\) -->/\1/' web.xml
但我认为单线更清洁(?)
这很相似:matching a line that doesn't contain specific text with regular expressions
【问题讨论】: