删除多行注释 [英] Remove multi-line comments
问题描述
我如何删除所有的意见,如果他们开始与/ *带*号结束/
我曾尝试以下。它的工作原理一行注释。
How do I remove all comments if they start with /* and end with */ I have tried the following. It works for one line comment.
sed '/\/\*/d'
但它不会删除多行注释。例如用于第二和第三线不会被删除。
But it does not remove multiline comments. for e.g. the second and third lines are not removed.
/*!50500 PARTITION BY RANGE (TO_SECONDS(date_time ))
PARTITION 20120102parti VALUES LESS THAN (63492681600),
(PARTITION 20120101parti VALUES LESS THAN (63492595200) */ ;
在上面的例子中,我需要的保留最后。注释结束符号后
In the above example, I need to retain the last ; after the closing comment sign.
推荐答案
如果这是一个C文件,那么你必须使用C preprocessor这与其他工具的组合暂时禁用特定preprocessor功能像扩大#定义或#包括所有其他方法将在边缘的情况下失败。这将为所有情况:
If this is in a C file then you MUST use a C preprocessor for this in combination with other tools to temporarily disable specific preprocessor functionality like expanding #defines or #includes, all other approaches will fail in edge cases. This will work for all cases:
[ $# -eq 2 ] && arg="$1" || arg=""
eval file="\$$#"
sed 's/a/aA/g;s/__/aB/g;s/#/aC/g' "$file" |
gcc -P -E $arg - |
sed 's/aC/#/g;s/aB/__/g;s/aA/a/g'
把它放在一个shell脚本,并与你想要解析的文件的名字称呼它,用标志像-ansipfixed可选$ P $指定C标准提出申请。
Put it in a shell script and call it with the name of the file you want parsed, optionally prefixed by a flag like "-ansi" to specify the C standard to apply.
这篇关于删除多行注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!