删除C源文件中的注释等

时间:2016-10-07 23:30:19

标签: 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(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;
}

2 个答案:

答案 0 :(得分:2)

如果没有语言解析器,你就无法做到。不要浪费你的时间尝试一些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  }

gcc周围的sed脚本会隐藏__中的所有#gcc,因此它不会扩展像#include这样的结构__FILENAME__

-ansigcc之类的参数,如果它没有根据您的喜好解析您的C风格,那么您正在使用哪种C标准。

答案 1 :(得分:0)

这对你也有帮助

org.apache.cxf.service.factory.ServiceConstructionException: Failed to create service.
    at org.apache.cxf.wsdl11.WSDLServiceFactory.create(WSDLServiceFactory.java:170)
    at org.apache.cxf.ws.security.trust.AbstractSTSClient.createClient(AbstractSTSClient.java:657)
    at org.apache.cxf.ws.security.trust.AbstractSTSClient.getClient(AbstractSTSClient.java:480)
    ...
Caused by: org.apache.ws.commons.schema.XmlSchemaException: Unable to locate imported document at 'https://...&xsd=ws-trust-1.3.xsd', relative to 'https://...#types1'.
    at org.apache.cxf.catalog.CatalogXmlSchemaURIResolver.resolveEntity(CatalogXmlSchemaURIResolver.java:76)
    at org.apache.ws.commons.schema.SchemaBuilder.resolveXmlSchema(SchemaBuilder.java:684)
    at org.apache.ws.commons.schema.SchemaBuilder.handleImport(SchemaBuilder.java:538)
    at org.apache.ws.commons.schema.SchemaBuilder.handleSchemaElementChild(SchemaBuilder.java:1516)
    at org.apache.ws.commons.schema.SchemaBuilder.handleXmlSchemaElement(SchemaBuilder.java:659)
    at org.apache.ws.commons.schema.XmlSchemaCollection.read(XmlSchemaCollection.java:551)
    at org.apache.cxf.common.xmlschema.SchemaCollection.read(SchemaCollection.java:129)
    at org.apache.cxf.wsdl11.SchemaUtil.extractSchema(SchemaUtil.java:140)
    at org.apache.cxf.wsdl11.SchemaUtil.getSchemas(SchemaUtil.java:73)
    at org.apache.cxf.wsdl11.SchemaUtil.getSchemas(SchemaUtil.java:65)
    at org.apache.cxf.wsdl11.SchemaUtil.getSchemas(SchemaUtil.java:60)
    at org.apache.cxf.wsdl11.WSDLServiceBuilder.getSchemas(WSDLServiceBuilder.java:378)
    at org.apache.cxf.wsdl11.WSDLServiceBuilder.buildServices(WSDLServiceBuilder.java:345)
    at org.apache.cxf.wsdl11.WSDLServiceBuilder.buildServices(WSDLServiceBuilder.java:209)
    at org.apache.cxf.wsdl11.WSDLServiceFactory.create(WSDLServiceFactory.java:162)
    ... 32 more

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;
}