convert C style comments to C++ style using sed

Try this:

sed 's,^\(.*\)/\*\([^/]*\)\*/$,\1//\2,'

This won't convert comments that contain embedded / characters. Alternatively, you could use:

sed 's,^\(.*\)/\*\(.*\)\*/$,\1//\2,'

This will do the wrong thing if you have two comments on the same line, e.g.

blah blah        /* comment1 */ blah /* comment2 */

will convert to

blah blah       // comment1 */ blah /* comment2

It might be possible to do better with a PCRE version of sed, as you could then use negative lookahead to test for embedded comments.

Note also that using , as the delimiter in the s command means that you don't have to escape all the / characters in the regexp -- that's the point of using some character other than / as the delimiter when the regexp will contain lots of /.


Probably the safest way is to first test for lines you don't want to affect and branch out of the script if you have a match.

sed '\|\*/.*/\*|b'

That's a little hard to read with all of the *stars in there, but basically if /* occurs after */ sed will quit executing its script, autoprint the line, and pull in the next line to begin the next line cycle. Any commands following that are not executed for a matching line.

Another way to do this is with test, which will similarly branch out of a script if it is provided no branch label following a successful s///ubstitution:

sed 's|/\*|&|2;t'

That attempts to replace the second occurrence of the pattern on the line with itself, and, if successful, it branches out in the same manner b does.

And so...

sed 's|/\*|&|2;s|\*/|&|2;t
     s|/\*\(.*\)\*/ *$|//\1|'

...will replace the first and only occurrence of /* with // on lines which end with the first and only occurrence of */ and any amount of trailing space characters. This works because t applies to any substitution occurring before it, and so if one or the other tests successful, sed branches out.

It may be that I blunder here, though, as I'm not very familiar with C or C++ and am uncertain what might happen in a /\*.*\*/.*\*/ case - which the above script branches away from . Perhaps you should instead be testing for only 2 */ or only 2 /*. Hopefully, at least though, I have managed to convey the concept to one who knows better.

Tags:

Sed