Open vieyahn2017 opened 1 year ago
# filename: strip_c_comment.awk # issue: awk -f scrip_c_comment.awk test.c BEGIN { FS="" } !(ignore_line && $NF == "\\") && !ignore_line-- { ignore_line = 0; for(i = 1; i <= NF; i++) { if (ignore_block) { if ($i $(i+1) == "*/") { ignore_block = 0 i++ # remove '*' } continue } if (!instr && $i $(i+1) == "/*") { ignore_block = 1 i++ # remove '/' continue } if (!instr && $i $(i+1) == "//") { ignore_line = ($NF == "\\")? 1: 0 break } if ($i == "\"") { instr = 1 - instr } printf($i) } printf("\n") }
用awk去除C语言注释。 https://blog.51cto.com/onlyzq/546456
简单点的
调用预处理器 cpp 来过滤
sed 's/^#/@/' main.c | cpp | sed -e '/#/d' -e 's/@/#/'
用awk去除C语言注释。 https://blog.51cto.com/onlyzq/546456