C comment stripper shell script? -> use sed pipeline

Maarten Litmaath maart at cs.vu.nl
Sun Mar 26 00:34:38 AEST 1989


jim at bilpin.UUCP (Jim G) writes:
\#{ 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

Aha! You're using a SHELL script! Well, in that case there's another word
for my `sed approach' :-)
No awk necessary. This pipeline is reasonably fast too!
Usage:
	sed -f Cstrip.1.sed foo.c | sed -f Cstrip.2.sed | sed -f Cstrip.3.sed

: This is a shar archive.  Extract with sh, not csh.
: This archive ends with exit, so do not worry about trailing junk.
: --------------------------- cut here --------------------------
PATH=/bin:/usr/bin:/usr/ucb
echo Extracting 'Cstrip.1.sed'
sed 's/^X//' > 'Cstrip.1.sed' << '+ END-OF-FILE ''Cstrip.1.sed'
X#n
Xs/\(.\)/\1\
X/g
Xs/$/==/p
+ END-OF-FILE Cstrip.1.sed
chmod 'u=rw,g=r,o=r' 'Cstrip.1.sed'
set `wc -c 'Cstrip.1.sed'`
count=$1
case $count in
27)	:;;
*)	echo 'Bad character count in ''Cstrip.1.sed' >&2
		echo 'Count should be 27' >&2
esac
echo Extracting 'Cstrip.2.sed'
sed 's/^X//' > 'Cstrip.2.sed' << '+ END-OF-FILE ''Cstrip.2.sed'
X#n
X/"/{
X	: L0
X	p
X	n
X	/"/{
X		p
X		b
X	}
X	/\\/{
X		p
X		n
X	}
X	b L0
X}
X/'/{
X	: L1
X	p
X	n
X	/'/{
X		p
X		b
X	}
X	/\\/{
X		p
X		n
X	}
X	b L1
X}
X/\\/{
X	p
X	n
X	p
X	b
X}
X/\//{
X	h
X	n
X	/*/{
X		: L2
X		n
X		: L3
X		/*/{
X			n
X			/\//b
X			b L3
X		}
X		b L2
X	}
X	H
X	g
X}
Xp
+ END-OF-FILE Cstrip.2.sed
chmod 'u=rw,g=r,o=r' 'Cstrip.2.sed'
set `wc -c 'Cstrip.2.sed'`
count=$1
case $count in
232)	:;;
*)	echo 'Bad character count in ''Cstrip.2.sed' >&2
		echo 'Count should be 232' >&2
esac
echo Extracting 'Cstrip.3.sed'
sed 's/^X//' > 'Cstrip.3.sed' << '+ END-OF-FILE ''Cstrip.3.sed'
X#n
X/==/{
X	g
X	s/\n//gp
X	s/.*//
X	x
X	b
X}
XH
+ END-OF-FILE Cstrip.3.sed
chmod 'u=rw,g=r,o=r' 'Cstrip.3.sed'
set `wc -c 'Cstrip.3.sed'`
count=$1
case $count in
40)	:;;
*)	echo 'Bad character count in ''Cstrip.3.sed' >&2
		echo 'Count should be 40' >&2
esac
exit 0
-- 
 Modeless editors and strong typing:   |Maarten Litmaath @ VU Amsterdam:
   both for people with weak memories. |maart at cs.vu.nl, mcvax!botter!maart



More information about the Comp.lang.c mailing list