shell pipeline to reverse the order of lines.

Tom Christiansen tchrist at convex.COM
Sun Mar 3 10:16:46 AEST 1991


>From the keyboard of iisakkil at niksula.hut.fi (Mika R Iisakkila):
:
:	Since this will probably be a never-ending thread, I'll post
:the most pervert way I could think of doing the deed:
:
:awk 'BEGIN{l=10000;}{printf("%d %s\n",l++,$0);}' | sort -r | colrm 1 6
:
:	Replace 'colrm 1 6' with 'cut -c7-' if you're on System V.
:Slight modifications required, if the file is large (what's the limit
:for scalar values in awk? Or does sort die first?). Hope that someone
:finds this at least marginally amusing...

Oh, so it's SILLY solutions you want, eh?  Well, try this one then:

    perl -e 'sub N {local($_); defined($_ = <>) && &N; print;} &N;' 

No extra processes, no limits on number of lines, lengths of lines, binary
versus text files, etc.  But it's best to unlimit stack for big files. :-)

Of course, if this weren't supposed to be a silly posting, I would have
just said 
    perl -e 'print reverse <>' file ...
and been done with it.

I can hear the cries about performance.  I wondered about this, too.  Now,
I know we've "fixed" tail -r so that it has an "infinite" buffer, unlike
many implementations, so I figured it would surely be much faster.  It's
not: it's only about 10% faster than the 2nd perl solution on /etc/termcap
(50% faster than the recursive one).  When run on /vmunix, it not only takes 
many times longer than the perl solution, it also gives the wrong answer.

Anyone have a solution not requiring a custom-made C program that actually
meets all these criteria:

    no restriction on number of lines
    no restriction on line length
    no restriction against 8-bit characters
    at least as fast as the "print reverse <>" perl solution

None of the other posted solutions that I've seen so far (I didn't run or
closely analyze the Icon or Python ones) have met all four of these, and
most don't make it past the first criterion.

I sure get tired of programs with arbitrary limitations, and that run
slowly besides.  Don't you?  


--tom
--
"UNIX was not designed to stop you from doing stupid things, because
 that would also stop you from doing clever things." -- Doug Gwyn

 Tom Christiansen                tchrist at convex.com      convex!tchrist



More information about the Alt.sources.d mailing list