changing c comments to c++ style with sed

Hi everyone,

I've got a problem with converting C comments ( /* */ ) into C++ style ( // ) in some source file with sed. So far I've dealt with comments on one line, but I don't know how to convert when it is over multiple lines ...

So I already have something like this:

comments.sed

s/\/\*/\/\//g
s/\*\//&\n/g
/\/\/.*[^*\/\n]$/a\something

I actually pick first /* and substitute it for // and then after */ put newlines. After then I would remove the / ones, but before that I've thought about putting // before every line in / */ multiple comment, but I don't know how (so the 3rd line in the code is only some idea). Do you have any idea? I would be really glad.

Thanks for every reply.

Why are you doing this? C style comments are acceptable in C++ , and in my view may be a better way of commenting multiple lines anyway

Well I need them each on one line because I will be processing them later ...

Why not process the comments blockwise, instead of this intermediate step to process them linewise ?

tyler_durden

I don't know how to do that... I just want to change comment like this:

/* Some comments1
   some comments2
   some comments3 */

to this:

// Some comments1
// some comments2
// some comments3

---------- Post updated 06-22-10 at 10:56 AM ---------- Previous update was 06-21-10 at 10:26 PM ----------

please anyone ??? :confused:

cat file.c

code code
code code

/* Some comments1
   some comments2
   some comments3 */
code code
code code
/* Some comments1
   some comments2
   some comments3 */
sed -e '/\/\*/,/\*\//s/^/\/\//g' -e '/\/\*/,/\*\//s/\/\*\|\*\///g' file.c

code code
code code

// Some comments1
//   some comments2
//   some comments3 
code code
code code
// Some comments1
//   some comments2
//   some comments3 

but take care, this may damage your code, if you use \* or */ for other purposes

1 Like

Bumping up posts or double posting is not permitted in these forums.
Please read the rules, which you agreed to when you registered, if you have not already done so.

Thank You.

Maybe you can try something like this:

awk '
/\/\*/{sub("\/\*","  ");f=1}
/\*\/$/{sub("\*\/","");print "//" $0;f=0;next}
f{print "//" $0;next}
1' file
1 Like

Thank you very much for replies and sorry for bumping up - I didn't realize that... I finally wrote some functional code (it isn't perfect but for my purposes it is enough) so I want to publish it, maybe it will be helpful for someone...

s/\*\//&\n/g
/\/\*.*\*\//s/\/\*/\/\//g
/[^\t ].*\/\*.*/s/\/\*/\n\/\*/g
/\/\*/,/\*\//s/^/\/\//g
/[^\/\/]\n\/\*/,/\*\//s/^/\/\//g
s/\/\/\/\//\/\//g
s/\/\*/\/\//g
s/\*\///g
/^[\/\/]/s/\(\/\/[ \t]*\)\/\//\1/g

So this convert comments in some source file from /* */ style to one-lined // ... Here is some commentary for every line:

1) Put newline after / of every comment
2) When there is one-lined /
/ I change / to //
3) If there is some multiline comment starting after code -> put newline (for simplify)
4), 5), 6) Putting // before multiline comments
7) Change starting /* in multiline comments to //
8), 9) Get rid of additional */ and //

It ain't that difficult, and definitely not so verbose -

$
$
$ cat -n testfile
     1  first line here...
     2  /*
     3    inside comment - line 3
     4    inside comment - line 4
     5  */
     6  line number 6
     7  line number 7
     8  /* a single line comment inside multi-line markers */
     9  line number 9
    10  /* and some more comments here
    11  and here
    12  and here - this being the last line */
    13  line number 13
    14  last line - number 14
$
$
$ perl -lne 'BEGIN{undef $/} while (/(\/\*.*?\*\/)/msg){print "\nMULTILINE COMMENT NUMBER ",++$i,":\n",$1}' testfile
 
MULTILINE COMMENT NUMBER 1:
/*
  inside comment - line 3
  inside comment - line 4
*/
 
MULTILINE COMMENT NUMBER 2:
/* a single line comment inside multi-line markers */
 
MULTILINE COMMENT NUMBER 3:
/* and some more comments here
and here
and here - this being the last line */
$
$

tyler_durden