[alt.sources] Blank trimmer

tneff@bfmny0.UUCP (Tom Neff) (08/29/89)

This is one of those nasty hacks purists get on your case for because
you can do it all with sed(1).  However I have a specialized application
where I use real long lines and I want fast performance, so I wrote this
little C filter that (a) trims blanks and tabs off the right side of
every input line and (b) allows no more than one blank line at a time
in the output.

I just thought I'd pass it along in case anyone else needs something
similar.  Hack away, I don't want to hear about it.

------ cut the GREEN wire first! ------ No, cut the RED wire f>>BOOM<< ------
#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create:
#	trimx.c
# This archive created: Mon Aug 28 15:50:36 1989
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f 'trimx.c'
then
	echo shar: "will not over-write existing file 'trimx.c'"
else
sed 's/^X//' << \SHAR_EOF > 'trimx.c'
X/*
X * trimx - blank trim from the right
X *
X * $Log:	trimx.c,v $
X * Revision 1.1  89/06/19  15:01:49  tneff
X * Initial revision
X *
X */
X
X#ident "@(#) $Id: trimx.c,v 1.1 89/06/19 15:01:49 tneff Exp $"
X
X#include <stdio.h>
X
Xmain()
X{
X	char s[65536];		/*  whatever  */
X	register char *p;
X
X	int jebl = 0;		/*  just emitted blank line  */
X
X	while (gets(s) != NULL)
X	{
X		for (p = s + strlen(s) - 1;
X			p >= s && (*p == ' ' || *p == '\t');
X			p--);
X		*(++p) = '\0';
X		if ((p != s) || !jebl)
X			puts(s);
X		jebl = (p == s);
X	}
X
X	fflush(stdout);
X	exit(0);
X}
SHAR_EOF
fi
exit 0
#	End of shell archive

-- 
"We walked on the moon --	((	Tom Neff
	you be polite"		 )) 	tneff@bfmny0.UU.NET

maart@cs.vu.nl (Maarten Litmaath) (08/29/89)

tneff@bfmny0.UUCP (Tom Neff) writes:
\This is one of those nasty hacks purists get on your case for because
\you can do it all with sed(1).  [...]

Indeed!  Heh heh heh...
----------8<----------8<----------8<----------8<----------8<----------
#!/bin/sh

tab="	"

SED="
	s/[ $tab][ $tab]*$//
	/^$/{
		p
	: gap
		n
		s/[ $tab][ $tab]*$//
		/^$/b gap
	}
	p
"

sed -n "$SED" ${1+"$@"}
----------8<----------8<----------8<----------8<----------8<----------
-- 
C, the programming language that's the same |Maarten Litmaath @ VU Amsterdam:
         in all reference frames.           |maart@cs.vu.nl, mcvax!botter!maart

jordan@Morgan.COM (Jordan Hayes) (08/30/89)

Tom Neff <tneff@bfmny0.UU.NET> writes:

	This is one of those nasty hacks purists get on your case for
	because you can do it all with sed(1).  However I have a
	specialized application where I use real long lines ...

>Xmain()
>X{
>X	char s[65536];		/*  whatever  */
>X
>X	while (gets(s) != NULL)

No, this is one of those nasty hacks purists get on your case about
because you use gets() instead of fgets() ...

/jordan