How can i use sed to replace all my C-style comments in a source file to C++ style.
All these:
int main() {
/* some comments */
...
to:
int main() {
// some comments
...
All comments are single line and there are none in between code like this:
int f(int x /*x-coordinate*/ );
so I tried this:
sed -i 's/ \/\* .* \*\ / \/\/* /g' src.c
but it leaves the file unchanged.
This post is similar, but I’m trying to understand sed’s expression syntax. Since “.” matches any character and ” * ” matches zero or more of some pattern. I assume “.*” matches any number of any character.
this will transform each line like this :
into a line like this:
If you have more comments on the same line, you can apply this more sophisticated parser, that converts a line like:
into
A more sophisticated case is when you have comments inside strings on a line, like in
call("/*string*/"). Here is a scriptc-comments.sed, to solve this problem:You save this script into a file c-comments.sed, and you call it like this: