Redian新闻
>
Help: convert // to /* */ in C code?
avatar
Help: convert // to /* */ in C code?# Unix - 噫吁兮,危乎高哉
R*r
1
I have a c source code can be compiled using cc in Unix
where comments can be used by either /*---- */ or //
How to compile it with gpcc where only /*---*/ can be used to
comments lines?
Is there easy way to convert // to /* */ in C code?
Apparent I cann't just use replace.
(Don't tell me to re-write it, I cannot afford to
re-write them all)
avatar
m*c
2

Try to see if "sed" can help:
sed -i'.bak' -e 's/^\([^\/]*\)\/\{2,\}\(.*\)$/\1\/*\2*\//g'filename.c
Above command will repalce "//..." to "/*...*/" in filename.c (and make a
backup of "filename.c" named "filename.c.bak").
To replace all files, use "find" and "sed" together:
find -iname '*.c' -print0 | xargs -0 sed -i'.bak' -e
's/^\([^\/]*\)\/\{2,\}\(.*\)$/\1\/*\2*\//g'

【在 R****r 的大作中提到】
: I have a c source code can be compiled using cc in Unix
: where comments can be used by either /*---- */ or //
: How to compile it with gpcc where only /*---*/ can be used to
: comments lines?
: Is there easy way to convert // to /* */ in C code?
: Apparent I cann't just use replace.
: (Don't tell me to re-write it, I cannot afford to
: re-write them all)

avatar
m*c
3

One more thing, before executing above command(s), backup everything :-) If
you run above commands twice, it will make backup of backups (in other words,
the original files will be overwritten.)

【在 m**c 的大作中提到】
:
: Try to see if "sed" can help:
: sed -i'.bak' -e 's/^\([^\/]*\)\/\{2,\}\(.*\)$/\1\/*\2*\//g'filename.c
: Above command will repalce "//..." to "/*...*/" in filename.c (and make a
: backup of "filename.c" named "filename.c.bak").
: To replace all files, use "find" and "sed" together:
: find -iname '*.c' -print0 | xargs -0 sed -i'.bak' -e
: 's/^\([^\/]*\)\/\{2,\}\(.*\)$/\1\/*\2*\//g'

avatar
q*e
4
if you only have one file, just open it with vi, then type command:
:%s/\/\/.*$/\/\* & \*\/g
then everything is fine. nOW YOU SAVE THE FILE WITH ANOTHER NAME.

【在 m**c 的大作中提到】
:
: One more thing, before executing above command(s), backup everything :-) If
: you run above commands twice, it will make backup of backups (in other words,
: the original files will be overwritten.)

avatar
X*r
5
just to remind the original poster that there are a few cases cannot
be processed by this kind of simple match and replacement method, e.g.
"//"
/*
//
*/
that's why backup is important.

【在 q***e 的大作中提到】
: if you only have one file, just open it with vi, then type command:
: :%s/\/\/.*$/\/\* & \*\/g
: then everything is fine. nOW YOU SAVE THE FILE WITH ANOTHER NAME.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。