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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting please explain this sed shell script to remove C++ comments.
# 1  
Old 02-19-2009
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 version went exactly a
# character at a time, hence the greater speed of this one. But the concept
# and especially the trick of building the line in hold space are entirely
# merit of Brian.

:loop

# This line is sufficient to remove C++ comments!
/^\/\// s,.*,,

/^$/{
x
p
n
b loop
}
/^"/{
:double
/^$/{
x
p
n
/^"/b break
b double
}

H
x
s,\n\(.[^\"]*\).*,\1,
x
s,.[^\"]*,,

/^"/b break
/^\\/{
H
x
s,\n\(.\).*,\1,
x
s/.//
}
b double
}

/^'/{
:single
/^$/{
x
p
n
/^'/b break
b single
}
H
x
s,\n\(.[^\']*\).*,\1,
x
s,.[^\']*,,

/^'/b break
/^\\/{
H
x
s,\n\(.\).*,\1,
x
s/.//
}
b single
}

/^\/\*/{
s/.//
:ccom
s,^.[^*]*,,
/^$/ n
/^\*\//{
s/..//
b loop
}
b ccom
}

:break
H
x
s,\n\(.[^"'/]*\).*,\1,
x
s/.[^"'/]*//
b loop
# 2  
Old 03-05-2009
Given that 2 weeks have passed and no one has replied, I suggest you consider emailing the authors with this question.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. Shell Programming and Scripting

Explain this shell script code.

Hi i am new to shell script can any one please explain me the code below which is written for execution of python scripts which are located in same folder. please explain the code line by line ls *.py > xx while do read myline || break python $myline done<xx Thanks Mukthyar.... (1 Reply)
Discussion started by: mukthar1255
1 Replies

3. Shell Programming and Scripting

explain while loop in ksh shell script

#!/bin/ksh log=ABCl log=EFG log=HIJ i=0 while <------ what is the meaning of ($i - lt 3) do print ${log} (( i=i+1 )) done (1 Reply)
Discussion started by: Bperl1967
1 Replies

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

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

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

7. Solaris

Pl explain the shell script code

if then ROLLBACK=1 ; elif then echo "Nothing to install!" ; echo "Exiting." ; exit 0; Plz explaing what is the ${1:-0} in if loop?:) (3 Replies)
Discussion started by: ysrikanth
3 Replies

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

9. Shell Programming and Scripting

explain me this shell script ...

currentSid=${TWO_TASK:-$ORACLE_SID} echo $currentSid this script returns value of ORACLE_SID but what i am not getting is what is ":-" doing ?? (1 Reply)
Discussion started by: zedex
1 Replies

10. UNIX for Dummies Questions & Answers

Remove comments...

It may be a no-brainer, but the answer is escaping me right now: I'm trying to write a little script to remove all comments from .c source... I was thinking sed, but I'm not a very strong regexp user (e.g. I suck with sed). I tried dumping the file into: sed -e 's/\/\* * \*\///g' and several... (1 Reply)
Discussion started by: LivinFree
1 Replies
Login or Register to Ask a Question