如何在bash中删除多行降价评论,例如下面的评论?
some text
<!-- QUESTION:
How do I remove everything
in-between these tags?
-->
some<!-- Including embedded single-line comments such as this --> text
我尝试了sed -e 's/<!--((.*?)\n?)+-->//g' $1
,它仅适用于单行,而cat $1 | tr '\n' '\r' | sed -e 's/<!--.*-->//g' | tr '\r' '\n'
则删除了第一个多行注释后的所有内容。
<!--((.*?)\n?)+-->
在我的文本编辑器中捕获所需的区域,但是
sed -e 's/<!--((.*?)\n?)+-->//g' $1
无效。
我可以发现使用C ++注释的其他示例太复杂,无法解码。
答案 0 :(得分:1)
您可以使用perl one-liner完成此操作。
Perl切换:
-0 sets the input record separator to the null character \0
-p prints the result of perl code
-e executes the following code
正则表达式:
g flag means global (perform the replacement as many times as possible)
s flag means treat the input as a multi-line string
Match the characters `<!--` followed by anything up to the characters `-->`
including anything after that till the newline. Replace that with nothing.
在行动:
perl -0pe 's|<!--.+?-->.*?\n||gs;' input
<强>输出强>:
some text
some text