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
# 8  
Old 06-22-2010
Thank you very much for replies and sorry for bumping up - I didn't realize that... I finally wrote some functional code (it isn't perfect but for my purposes it is enough) so I want to publish it, maybe it will be helpful for someone...

Code:
s/\*\//&\n/g
/\/\*.*\*\//s/\/\*/\/\//g
/[^\t ].*\/\*.*/s/\/\*/\n\/\*/g
/\/\*/,/\*\//s/^/\/\//g
/[^\/\/]\n\/\*/,/\*\//s/^/\/\//g
s/\/\/\/\//\/\//g
s/\/\*/\/\//g
s/\*\///g
/^[\/\/]/s/\(\/\/[ \t]*\)\/\//\1/g

So this convert comments in some source file from /* */ style to one-lined // ... Here is some commentary for every line:

1) Put newline after */ of every comment
2) When there is one-lined /* */ I change /* to //
3) If there is some multiline comment starting after code -> put newline (for simplify)
4), 5), 6) Putting // before multiline comments
7) Change starting /* in multiline comments to //
8), 9) Get rid of additional */ and //
# 9  
Old 06-22-2010
Quote:
Originally Posted by kolage
I don't know how to do that...
It ain't that difficult, and definitely not so verbose -

Code:
$
$
$ cat -n testfile
     1  first line here...
     2  /*
     3    inside comment - line 3
     4    inside comment - line 4
     5  */
     6  line number 6
     7  line number 7
     8  /* a single line comment inside multi-line markers */
     9  line number 9
    10  /* and some more comments here
    11  and here
    12  and here - this being the last line */
    13  line number 13
    14  last line - number 14
$
$
$ perl -lne 'BEGIN{undef $/} while (/(\/\*.*?\*\/)/msg){print "\nMULTILINE COMMENT NUMBER ",++$i,":\n",$1}' testfile
 
MULTILINE COMMENT NUMBER 1:
/*
  inside comment - line 3
  inside comment - line 4
*/
 
MULTILINE COMMENT NUMBER 2:
/* a single line comment inside multi-line markers */
 
MULTILINE COMMENT NUMBER 3:
/* and some more comments here
and here
and here - this being the last line */
$
$

tyler_durden
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