代码如下: #!/bin/sh cat $1 | sed -e ":b; */,//*/// { //*///! { $! { N; b b }; };*.*/*///d;};" | sed "/$/d" | sed '/.*$/d' | sed '/^$/d' 运行效果: $ cat comment.c /* comment here */ /* multi-line comment */ // another comment int main(int argc, char *argv[]) { return 0; } $ ./remove_comment comment.c int main(int argc, char *argv[]) { return 0; } $ 字符串中的注释标记无法处理,请慎用: $ cat comment.c /* comment here */ /* multi-line comment */ // another comment int main(int argc, char *argv[]) { printf("//"); return 0; } $ ./remove_comment comment.c int main(int argc, char *argv[]) { return 0; } $