The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
script command udi Shell Programming and Scripting 2 09-19-2008 01:15 PM
Command taken away from ksh script hgjdv Shell Programming and Scripting 2 02-16-2008 09:14 AM
Q: Recording shell script screen output using "script" command ? lalfonso.gomez Shell Programming and Scripting 4 01-18-2007 06:31 PM
reg script command.... deep UNIX for Dummies Questions & Answers 7 03-30-2005 08:22 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 10-26-2004
Registered User
 

Join Date: Oct 2004
Posts: 5
Sed Script/Command Help

I want to write a sed script/command that will delete my comments from a C++ program.

Basically, I need to delete all the "/* random text */" from a file. How do I go about doing this? I've been reading some stuff online and can't figure it out. Thank you for your help!
Reply With Quote
Forum Sponsor
  #2  
Old 10-27-2004
Registered User
 

Join Date: Sep 2003
Location: Canada
Posts: 56
That:
Code:
sed -e 's;//.*$;;'
or that:
Code:
sed -e 's/\/\/.*$//'
. Same thing.
Reply With Quote
  #3  
Old 10-27-2004
moxxx68's Avatar
Registered User
 

Join Date: Mar 2004
Posts: 301
Smile try this

sed 's/\/\*.*\*\///g' filename
moxxx68
__________________
moxxx68
http://www.estarinformado.com.ar/apicmaxmiel/bee-diez.gif
Reply With Quote
  #4  
Old 10-27-2004
Registered User
 

Join Date: Sep 2003
Location: Canada
Posts: 56
Oops. My examples were for // style comments...
Reply With Quote
  #5  
Old 10-27-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
Or to take care of // and /* */ as well as multiline /* */

Code:
#!/usr/bin/sed -nf

# This first case takes care of the following two 
# scenarios:
# 1:  /* a comment spanning an entire line */
# 2:  int code = 1; /* code + comment on same line */
# We are saying search between /* and */, and then
# substitute that for "nothing". So the syntax
# /start/,/end/ searches between start and end.
/\/\*/,/\*\// {
  s/\/\*.*\*\///g
}

# take care of multiline comments
# This deletes any lines inclusive. So if a multiline
# comment started on a line with code also, this would
# break.
/\/\*/,/\*\// {
  d
}

# now for // style comments
# This just says substitute // followed by any number
# of anything, followed by the end of the line, with nothing.
s/\/\/.*$//g

# print the rest
p
EDIT: Added "special" case above and comments

Cheers
ZB

Last edited by zazzybob; 10-27-2004 at 04:53 AM.
Reply With Quote
  #6  
Old 10-27-2004
moxxx68's Avatar
Registered User
 

Join Date: Mar 2004
Posts: 301
Quote:
s/\/\/.*$//g
dear zazzy bob
could you please explain this syntax..
i am not quite sure what it means..
also in the first example you used a , instead of .* what does the comma do?
thanx moxxx68
__________________
moxxx68
http://www.estarinformado.com.ar/apicmaxmiel/bee-diez.gif
Reply With Quote
  #7  
Old 10-27-2004
zazzybob's Avatar
Registered Geek
 

Join Date: Dec 2003
Location: Melbourne, Australia
Posts: 2,100
No problem. I have recommented my original example above to clarify a few things.

Cheers
ZB
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 11:02 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0