开阔未来
全部博文(102)
分类: LINUX
2010-03-31 13:09:53
#include#includeint main (int argc, char *argv[]){// not show\not show\not show// not show/* not show */int is; // not showint/* not show */ ms; /* not show */double ds; // not show\not show\not showdouble dm; /* ...not shownot show */ float fs; /*** now show*/float/**/ fm;char cs[] = "aaa // /***/";char cm1[] = /* not show */"hello*/";char cm2[] = "/*redraiment"/* not show */;/* printf("/////"); */return EXIT_SUCCESS;}
# filename: strip_c_comment.awk# issue: awk -f scrip_c_comment.awk test.cBEGIN { FS="" }!(ignore_line && $NF == "\\") && !ignore_line-- {ignore_line = 0;for(i = 1; i <= NF; i++) {if (ignore_block) {if ($i $(i+1) == "*/") {ignore_block = 0i++ # remove '*'}continue}if (!instr && $i $(i+1) == "/*") {ignore_block = 1i++ # remove '/'continue}if (!instr && $i $(i+1) == "//") {ignore_line = ($NF == "\\")? 1: 0break}if ($i == "\"") {instr = 1 - instr}printf($i)}printf("\n")}
#include#includeint main (int argc, char *argv[]){int is;int ms;double ds;double dm;float fs;float fm;char cs[] = "aaa // /***/";char cm1[] = "hello*/";char cm2[] = "/*redraiment";return EXIT_SUCCESS;}
chinaunix网友2010-03-31 13:28:00
还可以: sed -e "s@\/\*.*\*\/@@g" 1.txt|sed -e "/^$/d" <<< 删除 /* asdfasdf */ C注释里的东西,同时去除空行 sed -e "s@\/\*\(.*\)\*\/@\1@g" 1.txt|sed -e "/^$/d" <<<< 将 /* abcd */ 的 /* 和 */ 去掉,只留下 abcd