2

I am trying to remove comments from a file which may be in any part of a line and span multiple lines.

 struct my_struct{
  field1;
  field2; /** comment 1
  */ field3;
  /* comment 2 */
 } struct_name;

I need to get

struct my_struct{
  field1;
  field2; 
  field3;
 } struct_name;

I tried using

grep -o '[^/*]*[^*/]' 

to remove any text between matching /* and */, but it is eliminating the comment symbols but not the text in between. What is the correct way? If there is another way using 'sed', it would be nice to know that too.

kris
  • 209

2 Answers2

3

You can do it with a C compiler : :

gcc -fpreprocessed -dD -E -P file.c

check man gcc

  • Some discussion on the ups and downs of using cpp for that, here: http://unix.stackexchange.com/a/297375/170373 – ilkkachu Oct 20 '16 at 21:21
  • Yes, check this one especially: http://unix.stackexchange.com/questions/297346/how-can-i-delete-all-characters-falling-under-including/297375#comment522655_297375 – Gilles Quénot Oct 20 '16 at 21:24
  • This too: http://unix.stackexchange.com/questions/297346/how-can-i-delete-all-characters-falling-under-including/297375#comment523813_297375 – ilkkachu Oct 20 '16 at 21:25
2

If you just want to remove anything between /* and */, and ignore all the quirks of the C language, like C99-style //-comments, quoted strings and backslash-escapes of newlines, then a simple Perl-regex should do:

perl -0777 -pe 's,/\*.*?\*/,,gs' inputfile
ilkkachu
  • 138,973