Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex SED删除C程序注释_Regex_Linux_Sed - Fatal编程技术网

Regex SED删除C程序注释

Regex SED删除C程序注释,regex,linux,sed,Regex,Linux,Sed,我需要在linux中使用sed的C程序中删除注释行,假设每个注释行包含start和end标记,前后没有任何其他语句 例如,下面的代码: /* a comment line in a C program */ printf("It is /* NOT a comment line */\n"); x = 5; /* This is an assignment, not a comment line */ [TAB][SPACE] /* another empty comment line here

我需要在linux中使用sed的C程序中删除注释行,假设每个注释行包含start和end标记,前后没有任何其他语句

例如,下面的代码:

/* a comment line in a C program */
printf("It is /* NOT a comment line */\n");
x = 5; /* This is an assignment, not a comment line */
[TAB][SPACE] /* another empty comment line here */
/* another weird line, but not a comment line */ y = 0;
变成

printf("It is /* NOT a comment line */\n");
x = 5; /* This is an assignment, not a comment line */
/* another weird line, but not a comment line */ y = 0;
我知道这个正则表达式

^\s?\/\*.*\*\/$
匹配我需要删除的行。但是,以下命令:

sed -i -e  's/^\s?\/\*.*\*\/$//g' filename
这不管用

我不太确定我做错了什么

谢谢你的帮助。

这样做:

$ sed -e  '/^\s*\/\*.*\*\/$/d' file
printf("It is /* NOT a comment line */\n");
x = 5; /* This is an assignment, not a comment line */
/* another weird line, but not a comment line */ y = 0;
注:

  • ^\s?
    匹配零个或一个空格。看起来您想要匹配零个或一个或多个空格。因此,我们使用
    ^\s*

  • 由于您希望删除这些行而不是用空行替换它们,因此要使用的命令是
    d
    进行删除

  • 不必用
    /
    分隔正则表达式。我们可以使用
    |
    ,例如:

    sed -e  '\|^\s*/\*.*\*/$|d' file
    
    这样就无需转义
    /
    。根据在正则表达式中出现的次数,
    /
    ,这可能更简单,也可能不更清晰


  • 您正在做的是用空字符串替换正则表达式

    sed -i -e  's/^\s?\/\*.*\*\/$//g' filename
    
    这意味着

    sed -i -'s/pattern_to_find/replacement/g' : g means the whole file.
    
    您需要做的是删除带有正则表达式的行

    sed -i -e  '/^\s?\/\*.*\*\/$/d' filename
    

    这可能是您正在寻找的:

    $ awk '{o=$0; gsub(/\*\//,"\n"); gsub(/\/\*[^\n]*\n/,"")} NF{print o}' file
    printf("It is /* NOT a comment line */\n");
    x = 5; /* This is an assignment, not a comment line */
    /* another weird line, but not a comment line */ y = 0;
    /* first comment */ non comment /* second comment */
    
    以上操作已在此输入文件上运行:

    $ cat file
    /* a comment line in a C program */
    printf("It is /* NOT a comment line */\n");
    x = 5; /* This is an assignment, not a comment line */
         /* another empty comment line here */
    /* another weird line, but not a comment line */ y = 0;
    /* first comment */ non comment /* second comment */
    

    并且使用awk,因为一旦你通过了一个简单的s/旧/新/所有东西,使用awk就更容易(更高效、更便携等)。以上内容将删除任何空行-如果这是一个问题,则更新您的示例输入/输出以包含该内容,但这是一个简单的解决方法。

    您应该在示例中包含
    /*第一条注释*/非注释/*第二条注释*/
    ,因为这对于sed脚本来说很难正确处理。你现有的答案中没有一个能正确处理,他们都会认为这是一个评论行。