Skip to main content
2 of 9
sample added
bu5hman
  • 4.9k
  • 2
  • 16
  • 29

This should get past the issue of code bracketed between comments

sed  -E '/^\s*\/\*/!bx ; /\*\/\s*$/!bx ; /\*\/[^;]+.*\/\*/bx ; d;  :x'

If a line doesn't start with a comment marker led only by whitespace then it starts with code so branch past the delete to x

/^\s*\/\*/!bx    

If the line starting as a comment doesn't end as a comment followed only by whitespace then there is code at the end so branch past the delete to x

/\*\/\s*$/!bx

If the comment line contains a (second) comment end followed by statement ; and eventually by another comment start then there is code inside so branch past the delete to x

/\*\/[^;]+.*\/\*/bx

Since we found no code then delete

d

Otherwise finish without doing anything

x

Tested on

/* Comment 1 */
printf("It is /* Comment 2 */\n");
x = 5; /* Comment 3 */
            /* Comment 4 */
/* Comment 5 */ y = 0;
            /*
             * Comment 6
             */
            // Comment 7
/* between two comments */ x = 0; /* could be some code */

Output is

printf("It is /* Comment 2 */\n");
x = 5; /* Comment 3 */
/* Comment 5 */ y = 0;
            /*
             * Comment 6
             */
            // Comment 7
/* between two comments */ x = 0; /* could be some code */
bu5hman
  • 4.9k
  • 2
  • 16
  • 29