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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed script, changing all C-comments to C++-comments
# 1  
Old 11-21-2009
Sed script, changing all C-comments to C++-comments

I must write a script to change all C++ like comments:

Code:
// this is a comment

to this one

Code:
/* this is a comment */

How to do it by sed? With file:

Code:
#include <cstdio>
using namespace std; //one

// two
int main() {
  printf("Example"); //    three
}//four

the result should be:


Code:
#include <cstdio>
using namespace std; /*one*/

/* two*/
int main() {
  printf("Example"); /*    three*/
}/*four*/

I don't know how to append */ on the end of line. Can anyone help me?

Best regards,
Lucas

Last edited by black_hawk; 11-21-2009 at 08:20 PM..
# 2  
Old 11-21-2009
Try:

Code:
# sed "s@//\(.*\)@/*\1*/@g" file1  
#include <cstdio>
using namespace std; /*one*/

/* two*/
int main() {
  printf("Example"); /*    three*/
}/*four*/

Of course, this doesn't cater for when there are // inside strings...
# 3  
Old 11-21-2009
Great, thanks a lot. I haven't any // in strings, so it works.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script to find comments in file

As I stated in a previous thread - I'm a newbie to Unix/Linux and programming. I'm trying to learn the basics on my own using a couple books and the exercises provided inside. I've reached an exercise that has me stumped. I need to write a bash script that will will read in a file and print the... (11 Replies)
Discussion started by: ksmarine1980
11 Replies

2. Shell Programming and Scripting

How to remove comments from a bash script?

I would like to remove comments from a bash script. In addition, I would like to remove lines that consist of only white spaces, and to remove blank lines. #!/bin/bash perl -pe 's/ *#.*$//g' $1 | grep -v ^]*$ | perl -pe 's/ +/ /g' > $2 # # $1 INFILE # $2 OUTFILE The above code... (10 Replies)
Discussion started by: LessNux
10 Replies

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

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

5. Shell Programming and Scripting

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 ... (8 Replies)
Discussion started by: kolage
8 Replies

6. Shell Programming and Scripting

print only comments

I want to write a shell script which it takes as argument a java file or a c++ file (.java or .cpp). It will check if the file is type of java or c++, else it ends with error message. If all are ok, it will call awk that prints only the comments that the java or c++ file contains, grouping and... (5 Replies)
Discussion started by: Mark_orig
5 Replies

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

8. Shell Programming and Scripting

Script for updating the comments field on /etc/passwd on redhat linux

Hi there, I have more that 300 servers that I need to updated the comments field on /etc/passwd for users that have a blank comments fields. The users have accounts on different servers. I have created a list of these users on a text file called update_passwd.txt. I need a script that will... (6 Replies)
Discussion started by: Linux Duke
6 Replies

9. Shell Programming and Scripting

How to write comments in Korn shell script

What is the syntax for writing comment code in Korn shell scripts? (1 Reply)
Discussion started by: sasaliasim
1 Replies
Login or Register to Ask a Question