Awk 删除C源文件中的注释等

Awk 删除C源文件中的注释等,awk,sed,Awk,Sed,我想设计一个sed和awk管道,删除所有注释和空行,向C源文件添加行号,并将输出保存到new_example.C。到目前为止,我唯一能完成的是's/[/**/////g',它只删除“/”和“/”而不是中间的文本 //this is a comment #include <stdio.h> #include <stdlib.h> /* this is the main program remove this line and this line */ int main(i

我想设计一个sed和awk管道,删除所有注释和空行,向C源文件添加行号,并将输出保存到new_example.C。到目前为止,我唯一能完成的是's/[/**/////g',它只删除“/”和“/”而不是中间的文本

//this is a comment
#include <stdio.h>
#include <stdlib.h>
/* this is the main program
remove this line
and this line
*/

int main(int argc, char *argv[])
{
    //this is another comment
    char *path;
    int numbers[10];
    int *a1;
    a1= malloc(10*sizeof(int));

    float *a2;
    a2 = malloc(10*sizeof(float));

    a1[2] = 10;
    a2[4] = 3.14;
    free(a1 );
    free(a2);

    return 0;
}
//这是一条注释
#包括
#包括
/*这是主要节目
删除此行
这条线呢
*/
int main(int argc,char*argv[])
{
//这是另一个评论
字符*路径;
整数[10];
int*a1;
a1=malloc(10*sizeof(int));
浮动*a2;
a2=malloc(10*sizeof(浮动));
a1[2]=10;
a2[4]=3.14;
免费(a1);
免费(a2);
返回0;
}

没有语言解析器,您无法完成这项工作。不要浪费时间尝试一些sed或awk或任何脚本攻击——在某些情况下,即使您现在无法弄清楚它们是什么,它也会失败

类似这样的操作将执行您想要的操作,使用
gcc
解析C:

$ sed 's/a/aA/g; s/__/aB/g; s/#/aC/g' file.c |
        gcc -P -E - |
        sed 's/aC/#/g; s/aB/__/g; s/aA/a/g' |
        cat -n
 1  #include <stdio.h>
 2  #include <stdlib.h>
 3  int main(int argc, char *argv[])
 4  {
 5      char *path;
 6      int numbers[10];
 7      int *a1;
 8      a1= malloc(10*sizeof(int));
 9      float *a2;
10      a2 = malloc(10*sizeof(float));
11      a1[2] = 10;
12      a2[4] = 3.14;
13      free(a1 );
14      free(a2);
15      return 0;
16  }
$sed's/a/aA/g;s/aB/g;s/#/aC/g’file.c|
gcc-P-E-|
sed's/aC/#/g;s/aB/g;s/aA/a/g'|
n类
1#包括
2#包括
3 int main(int argc,char*argv[])
4  {
5字符*路径;
6个整数[10];
7 int*a1;
8 a1=malloc(10*sizeof(int));
9*a2;
10 a2=malloc(10*sizeof(浮动));
11 a1[2]=10;
12 a2[4]=3.14;
13免费(a1);
14免费(a2);
15返回0;
16  }
gcc
周围的sed脚本用于隐藏
gcc
中的所有
\uuuuu
,因此它不会扩展
\include
\uuu文件名
之类的结构


对于您正在使用的任何C标准,如果默认情况下它不能根据您的喜好解析您喜欢的C语言风格,请将诸如
-ansi
之类的参数添加到
gcc

如果没有语言解析器,您就无法做到这一点。不要浪费时间尝试一些sed或awk或任何脚本攻击——在某些情况下,即使您现在无法弄清楚它们是什么,它也会失败

类似这样的操作将执行您想要的操作,使用
gcc
解析C:

$ sed 's/a/aA/g; s/__/aB/g; s/#/aC/g' file.c |
        gcc -P -E - |
        sed 's/aC/#/g; s/aB/__/g; s/aA/a/g' |
        cat -n
 1  #include <stdio.h>
 2  #include <stdlib.h>
 3  int main(int argc, char *argv[])
 4  {
 5      char *path;
 6      int numbers[10];
 7      int *a1;
 8      a1= malloc(10*sizeof(int));
 9      float *a2;
10      a2 = malloc(10*sizeof(float));
11      a1[2] = 10;
12      a2[4] = 3.14;
13      free(a1 );
14      free(a2);
15      return 0;
16  }
$sed's/a/aA/g;s/aB/g;s/#/aC/g’file.c|
gcc-P-E-|
sed's/aC/#/g;s/aB/g;s/aA/a/g'|
n类
1#包括
2#包括
3 int main(int argc,char*argv[])
4  {
5字符*路径;
6个整数[10];
7 int*a1;
8 a1=malloc(10*sizeof(int));
9*a2;
10 a2=malloc(10*sizeof(浮动));
11 a1[2]=10;
12 a2[4]=3.14;
13免费(a1);
14免费(a2);
15返回0;
16  }
gcc
周围的sed脚本用于隐藏
gcc
中的所有
\uuuuu
,因此它不会扩展
\include
\uuu文件名
之类的结构

对于您正在使用的任何C标准,如果默认情况下它不能根据您的喜好解析您的C风格,请在
gcc
中添加参数,例如
-ansi

这也可能对您有所帮助

gcc -fpreprocessed -E   test.c | sed '/^\s*$/d'
gcc-fpreprocessed-E test.c
-删除注释

sed'/^\s*$/d'
-删除空行

测试输入文件

[akshay@localhost tmp]$ cat test.c
//this is a comment
#include <stdio.h>
#include <stdlib.h>
/* this is the main program
remove this line
and this line
*/

int main(int argc, char *argv[])
{
    //this is another comment
    char *path;
    int numbers[10];
    int *a1;
    a1= malloc(10*sizeof(int)); // here is comment

    float /*comment*/ *a2;
    a2 = malloc(10*sizeof(float)); /* comment*/

    a1[2] = 10;
    a2[4] = 3.14;
    free(a1 );
    free(a2);

    return 0;
}
[akshay@localhosttmp]$cat test.c
//这是一个评论
#包括
#包括
/*这是主要节目
删除此行
这条线呢
*/
int main(int argc,char*argv[])
{
//这是另一个评论
字符*路径;
整数[10];
int*a1;
a1=malloc(10*sizeof(int));//这里是注释
浮动/*注释*/*a2;
a2=malloc(10*sizeof(float));/*注释*/
a1[2]=10;
a2[4]=3.14;
免费(a1);
免费(a2);
返回0;
}
输出

[akshay@localhost tmp]$ gcc -fpreprocessed -E   test.c | sed '/^\s*$/d'
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    char *path;
    int numbers[10];
    int *a1;
    a1= malloc(10*sizeof(int));
    float *a2;
    a2 = malloc(10*sizeof(float));
    a1[2] = 10;
    a2[4] = 3.14;
    free(a1 );
    free(a2);
    return 0;
}
[akshay@localhosttmp]$gcc-fpreprocessed-E test.c | sed'/^\s*$/d'
#包括
#包括
int main(int argc,char*argv[])
{
字符*路径;
整数[10];
int*a1;
a1=malloc(10*sizeof(int));
浮动*a2;
a2=malloc(10*sizeof(浮动));
a1[2]=10;
a2[4]=3.14;
免费(a1);
免费(a2);
返回0;
}
这也可能对您有所帮助

gcc -fpreprocessed -E   test.c | sed '/^\s*$/d'
gcc-fpreprocessed-E test.c
-删除注释

sed'/^\s*$/d'
-删除空行

测试输入文件

[akshay@localhost tmp]$ cat test.c
//this is a comment
#include <stdio.h>
#include <stdlib.h>
/* this is the main program
remove this line
and this line
*/

int main(int argc, char *argv[])
{
    //this is another comment
    char *path;
    int numbers[10];
    int *a1;
    a1= malloc(10*sizeof(int)); // here is comment

    float /*comment*/ *a2;
    a2 = malloc(10*sizeof(float)); /* comment*/

    a1[2] = 10;
    a2[4] = 3.14;
    free(a1 );
    free(a2);

    return 0;
}
[akshay@localhosttmp]$cat test.c
//这是一个评论
#包括
#包括
/*这是主要节目
删除此行
这条线呢
*/
int main(int argc,char*argv[])
{
//这是另一个评论
字符*路径;
整数[10];
int*a1;
a1=malloc(10*sizeof(int));//这里是注释
浮动/*注释*/*a2;
a2=malloc(10*sizeof(float));/*注释*/
a1[2]=10;
a2[4]=3.14;
免费(a1);
免费(a2);
返回0;
}
输出

[akshay@localhost tmp]$ gcc -fpreprocessed -E   test.c | sed '/^\s*$/d'
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
    char *path;
    int numbers[10];
    int *a1;
    a1= malloc(10*sizeof(int));
    float *a2;
    a2 = malloc(10*sizeof(float));
    a1[2] = 10;
    a2[4] = 3.14;
    free(a1 );
    free(a2);
    return 0;
}
[akshay@localhosttmp]$gcc-fpreprocessed-E test.c | sed'/^\s*$/d'
#包括
#包括
int main(int argc,char*argv[])
{
字符*路径;
整数[10];
int*a1;
a1=malloc(10*sizeof(int));
浮动*a2;
a2=malloc(10*sizeof(浮动));
a1[2]=10;
a2[4]=3.14;
免费(a1);
免费(a2);
返回0;
}
正确删除所有注释将是一件棘手的事情。我不确定你是否可以在awk和sed中完成,你可能需要lex。正确地删除所有注释将是一件棘手的事情。我不确定你能不能用awk和sed,你可能需要莱克斯。