我需要在linux下使用sed删除C程序中的注释行,假设每个注释行包含开始和结束标记,而前后没有任何其他语句。SED删除C程序注释
例如,下面的代码:
/* 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脚本难以正确处理。你现有的答案都不能正确处理,他们都认为这是一条评论线。 –
https://unix.stackexchange.com/questions/297346/how-can-i-delete-all-characters-falling-under-including – bishop