Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!csd4.milw.wisc.edu!lll-winken!uunet!mcvax!ukc!icdoc!bilpin!jim From: jim@bilpin.UUCP (Jim G) Newsgroups: comp.lang.c Subject: Re: Want a way to strip comments from a C program Message-ID: <1467@bilpin.UUCP> Date: 22 Mar 89 12:35:15 GMT Organization: SRL, London, England Lines: 38 #{ v_langC.1 } Lots of postings on how not do this, and how other peoples suggestions won't work, but something of a shortage of answers. Weep no more ... if( Followup ~ /doesn't work with/ && Followup !~ /solution is/ ) print Followup > "/dev/null" :-) #{ zapcom.sh } # Remove comments from a C program # sed removes comment strings which begin and end on the same line # awk removes comment strings which extend across multiple lines # sed/awk both handle nesting of comments within their context sed -e ':nest' \ -e 's?/\*[^/\*][^/\*]*\*/??g' \ -e 'tnest' yourinput.c \ | awk ' /\/\*/ { if( COML == 0 ) print substr( $0, 1, index( $0, "/*" )-1 ) for( F=1; F<=NF; F++ ) if( $F == "/*" ) COML++ } /\*\// { REST = $0 for( F=1; F<=NF; F++ ) if( $F == "*/" ) { COML-- ; REST = substr( REST, index( REST, "*/" ) ) } if( COML == 0 ) print substr( REST, 3 ) next } COML==0 { print } ' > youroutput.c -- This line has been intentionally left blank.