Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace comments using shell script 1

Status
Not open for further replies.

saikrishna80

Programmer
Dec 18, 2003
5
0
0
FR
Hi all,
I have a source code which contains C++ comments
like
//sprintf(chL_errbuf, " In parse_string_nw_event, received [%s]",ch_msg );

But I want a shell script which will replace this part of code above with C comments.(maybe you can use sed)

/* sprintf(chL_errbuf, " In parse_string_nw_event, received [%s]",ch_msg ); */

Please reply.

Regards
Sai.
 
Do you have any odd cases which might look like C comments inside a C++ comment

For example
Code:
// /*ignore*/printf( "hello world\n" );
Simplistic search/replace will result in code which no longer compiles.



--
 
Something like this ?
sed '/^\/\//{;s!^//!/* !;s!$! */!;}'

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I don't understand the {;s!^//!/* !;s!$! */!;}-part, of PHV's post, but I guess the first part will only search for comments starting in the first line: /^\/\/.

While temporarly ignoring salems warning, you could do:
Code:
sed 's/\/\/\(.*\)/\/\*\1\*\//g'

To salems warning: if you first look for nested comments by grep, but there is a second seldom risk of breaking the code: a comment inside a literal string:
Code:
char * foo = "We like // comments";
but the problem is deeper than desribed by salem.
My sed-script would modify this:

Code:
// /*ignore*/printf( "hello world\n" );
to
Code:
/* /*ignore*/ printf( "hello world\n" ); */
but look at this:
Code:
/* 
multi line comment
// printf( "hello world\n" );
*/
will change to
Code:
/* 
multi line comment
/*printf( "hello world\n" );*/
*/

So grep for comments in comments:
Code:
grep "\/*" *.c
and
for comments in String literals
Code:
grep "\"[^\"]\*//" *.c
and see, if some files are matching.

If not, you could try the script.
But a) make a backup, because I don't tested my advices.
And b) make a backup, since there are other possibilities, which aren't covered yet.
1. A String, containing a block-comment, containing a line-comment.
2. define:
#define LINE_COMMENT //
#define BLOCK_COMMNENT_OPEN /*
3. include: see 2.

If all these topics are covered by the part of PHVs post, I didn't understand - sorry.

seeking a job as java-programmer in Berlin:
 
stefanwagner, my sed script deals with ALL (not only the 1st) lines starting with // and don't consider comments starting elsewhere.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top