Raw Input
Data-1 Data-2
Data-3/* comment */ Data-4/*
comment comment */ Data-5
Data-6 Data-7
/* comment */Data-8
Data-9
Data-10 /* comment */ Data-11 /* comment */ Data-12
Desired Output
Data-1 Data-2
Data-3 Data-4 Data-5
Data-6 Data-7
Data-8
Data-9
Data-10  Data-11  Data-12
Script and Comments
Script1
[ 1] /\/\*/{
[ 2] :loop
[ 3] s|\(.*\)/\*.*\*/|\1|
[ 4] t loop
[ 5] /\/\*/{
[ 6] N
[ 7] b loop
[ 8] }
[ 9] }
Comments
  1. 'Pattern space' is abbreviated to 'PS'.
  2. Assume that no nested comments exist in your C program, and '/*' and '*/' are well-balanced.
  3. Each time we found a line contains '/*', we first remove all comments of that line. Since sed does not provide non-greedy verison of '*', we have to remove comments from right to left. This is done via step [3] thru [4].
  4. If now PS contains '/*', this implies a multi-line comment. We have to repeat the following loop:
    • Join next line (step [6]).
    • Remove all comments (/* ... */) (step [7]->[2]->[3]->[4])
    until no single '/*' exists.
  5. When no '/*' exists in PS, we print it out and start a new cycle.
Script2
[ 1] :loop
[ 2] s|\(.*\)/\*.*\*/|\1|
[ 3] t loop
[ 4] /\/\*/!b
[ 5] N
[ 6] b loop
Comments
  1. This is a refined version.