changing c comments to c++ style with sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting changing c comments to c++ style with sed
# 1  
Old 06-21-2010
changing c comments to c++ style with sed

Hi everyone,

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 ...

So I already have something like this:

comments.sed
Code:
s/\/\*/\/\//g
s/\*\//&\n/g
/\/\/.*[^*\/\n]$/a\something

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.

Thanks for every reply.
# 2  
Old 06-21-2010
Why are you doing this? C style comments are acceptable in C++ , and in my view may be a better way of commenting multiple lines anyway
# 3  
Old 06-21-2010
Well I need them each on one line because I will be processing them later ...
# 4  
Old 06-21-2010
Quote:
Originally Posted by kolage
Well I need them each on one line because I will be processing them later ...
Why not process the comments blockwise, instead of this intermediate step to process them linewise ?

tyler_durden
# 5  
Old 06-22-2010
I don't know how to do that... I just want to change comment like this:

Code:
/* Some comments1
   some comments2
   some comments3 */

to this:

Code:
// Some comments1
// some comments2
// some comments3



---------- Post updated 06-22-10 at 10:56 AM ---------- Previous update was 06-21-10 at 10:26 PM ----------

please anyone ??? Smilie
# 6  
Old 06-22-2010
Code:
cat file.c

code code
code code

/* Some comments1
   some comments2
   some comments3 */
code code
code code
/* Some comments1
   some comments2
   some comments3 */

Code:
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
This User Gave Thanks to funksen For This Post:
# 7  
Old 06-22-2010
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.

Maybe you can try something like this:
Code:
awk '
/\/\*/{sub("\/\*","  ");f=1}
/\*\/$/{sub("\*\/","");print "//" $0;f=0;next}
f{print "//" $0;next}
1' file

This User Gave Thanks to Franklin52 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Changing time-stamp with sed

Hi ! I try to change a time-stamp hh:mm:ss allways to full ten-minutes. example: 12:51:03 to 12:50:03 sed 's/::/:{0-5}0:/g' file.txt but it will not work propperly, because the minute-decade will be replaced with the bracket-term {0-5}. Can someone please give me a hint? Thanks in... (6 Replies)
Discussion started by: IMPe
6 Replies

2. Shell Programming and Scripting

sed remove comments

I need to use sed to remove comments from files. I am using this, but it only works on comments that start at the beginning of the line. sed /^"\/\/"/d In most of the files I have comments like this: code // Comments or tab // Comments (5 Replies)
Discussion started by: gravesit
5 Replies

3. Shell Programming and Scripting

sed remove css comments

Is there a way that I can use sed to remove lines with css comments like this? /* comment */ (9 Replies)
Discussion started by: gravesit
9 Replies

4. Shell Programming and Scripting

Sed script, changing all C-comments to C++-comments

I must write a script to change all C++ like comments: // this is a comment to this one /* this is a comment */ How to do it by sed? With file: #include <cstdio> using namespace std; //one // two int main() { printf("Example"); // three }//four the result should be: (2 Replies)
Discussion started by: black_hawk
2 Replies

5. Shell Programming and Scripting

please explain this sed shell script to remove C++ comments.

#! /bin/sed -nf # Remove C and C++ comments, by Brian Hiles (brian_hiles@rocketmail.com) # Sped up (and bugfixed to some extent) by Paolo Bonzini (bonzini@gnu.org) # Works its way through the line, copying to hold space the text up to the # first special character (/, ", '). The original... (1 Reply)
Discussion started by: Priyaranjan
1 Replies

6. Shell Programming and Scripting

awk, sed, grep...weird style

my desired output is like this: so the thing is, I only need to show every of this part out but the frequency of that data is not fixed, so sometimes it may have 4 lines, or 6 lines or whatever in that file. However, the last line will always have empty space/line below it. (13 Replies)
Discussion started by: finalight
13 Replies

7. Shell Programming and Scripting

sed and changing the file itself

hello I have this: sed -e "s/install_location=....../g" -e "s/hostname=....../g" -e "s/server_name=....../y" input.txt it will display on the screen what have changed. however I want to change file input.txt. Any idea other than doing redirection (>) thx (2 Replies)
Discussion started by: melanie_pfefer
2 Replies

8. UNIX for Dummies Questions & Answers

Changing the order using sed

I have a text "abc def ghi" and I want to get it as "def abc ghi" I am using this echo "abc def ghi" | sed 's/\(*\)\(*\)/\2\1/' But I am not able to get the output, could anyone help me. Thanks (9 Replies)
Discussion started by: venu_nbk
9 Replies

9. UNIX for Dummies Questions & Answers

Changing text with sed?

Hi everyone, Having trouble with sed. I searched the board and found some stuff, but still unclear. I have a file named "userfile" which stores the users info in this form: email:username:password: I want the user to be able to change their password. i tried with sed s/oldpass/newpass/g... (2 Replies)
Discussion started by: primal
2 Replies
Login or Register to Ask a Question