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 ...
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.
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
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 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...
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 */
$
$