[alt.sources.d] shell pipeline to reverse the order of lines.

kyle@UUNET.UU.NET (Kyle Jones) (02/16/91)

After seeing a couple of programs in Perl and Icon to do this, I
feel compelled to post a shell pipeline solution to this problem.

   cat -n | sort -rn | sed 's/ *[0-9]*.//'

For System V folks whose cat(1) doesn't have -n, just use awk:

   awk '{ printf "%10d\t%s\n", NR, $0 }' | sort -rn | sed 's/ *[0-9]*.//'

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (02/16/91)

In article <9102151917.AA04419@wendy-fate.UU.NET> kyle@UUNET.UU.NET (Kyle Jones) writes:
>    cat -n | sort -rn | sed 's/ *[0-9]*.//'

If you care so much about it, write it in C. This version is several
times faster than any of the other versions posted; it's even 50% faster
than the ``tac'' that comes with SunOS. It only works on files, though,
so you have to create a temporary file if you want to use it off a pipe.

---Dan

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
extern char *malloc();

main()
{
 char *s;
 int i;
 int j;
 struct stat st;

 if (fstat(0,&st) == -1) exit(1);
 if (!(s = malloc(st.st_size + 3))) exit(2);
 if (read(0,s,st.st_size) < st.st_size) exit(3);
 i = j = st.st_size - 1;
 do
  {
   if (s[i] == '\n')
    {
     if (fwrite(s + i + 1,1,j - i,stdout) < j - i) exit(4);
     j = i;
    }
  }
 while(i--);
 if (fwrite(s,1,j + 1,stdout) < j + 1) exit(4);
 exit(0);
}

tale@rpi.edu (David C Lawrence) (02/16/91)

In <9102151917.AA04419@wendy-fate.UU.NET> kyle@UUNET.UU.NET (Kyle Jones):

   After seeing a couple of programs in Perl and Icon to do this, I
   feel compelled to post a shell pipeline solution to this problem.

And I an Emacs-Lisp one, though Kyle could have handled it himself.  I
actually wrote this several months ago.  It was the speediest of a few
very simple algorithms, but also very costly in memory usage.  It is
longer than it would be were it to just do the original task of
reversing a file, but working with a region adds a little more to its
length (and to me, its value).

(defun reverse-region (beg end)
  "Reverse the order of lines in a region.
From a program takes two point or marker arguments, BEG and END."
  (interactive "r")
  (if (> beg end)
      (let (mid) (setq mid end end beg beg mid)))
  (save-excursion
    ;; put beg at the start of a line and end and the end of one --
    ;; the largest possible region which fits this criteria
    (goto-char beg)
    (or (bolp) (forward-line 1))
    (setq beg (point))
    (goto-char end)
    ;; the test for bolp is for those times when end is on an empty line;
    ;; it is probably not the case that the line should be included in the
    ;; reversal; it isn't difficult to add it afterward.
    (or (and (eolp) (not (bolp))) (progn (forward-line -1) (end-of-line)))
    (setq end (point-marker))
    ;; the real work.  this thing cranks through memory on very large regions.
    (let (ll (do t))
      (while do
	(goto-char beg)
	(setq ll (cons (buffer-substring (point) (progn (end-of-line) (point)))
		       ll))
	(setq do (/= (point) end))
	(delete-region beg (if do (1+ (point)) (point))))
      (while (cdr ll)
	(insert (car ll) "\n")
	(setq ll (cdr ll)))
      (insert (car ll)))))

--
   (setq mail '("tale@cs.rpi.edu" "tale@ai.mit.edu" "tale@rpitsmts.bitnet"))

nazgul@alphalpha.com (Kee Hinckley) (02/17/91)

In article <3*-&S|^@rpi.edu> tale@rpi.edu (David C Lawrence) writes:
>In <9102151917.AA04419@wendy-fate.UU.NET> kyle@UUNET.UU.NET (Kyle Jones):
>
>   After seeing a couple of programs in Perl and Icon to do this, I
>   feel compelled to post a shell pipeline solution to this problem.
>
>And I an Emacs-Lisp one, though Kyle could have handled it himself.  I

It's clearly time to create alt.sources.showoff for people to post
unneeded solutions to already solved problems.
-- 
Alfalfa Software, Inc.		|	Poste:  The EMail for Unix
nazgul@alfalfa.com		|	Send Anything... Anywhere
617/646-7703 (voice/fax)	|	info@alfalfa.com

I'm not sure which upsets me more; that people are so unwilling to accept
responsibility for their own actions, or that they are so eager to regulate
everyone else's.

tale@rpi.edu (David C Lawrence) (02/18/91)

In <1991Feb17.021452.21206@alphalpha.com> nazgul@alphalpha.com (Kee Hinckley):

   It's clearly time to create alt.sources.showoff for people to post
   unneeded solutions to already solved problems.

Speak for your own needs, bub.  reverse-region solved a problem for me
that the other solutions did not.  In my environment, it is much more
useful to me than any of the other reversal schemes I have seen as
part of this thread.
--
    (setq mail '("tale@rpi.edu" "uupsi!rpi!tale" "tale@rpitsmts.bitnet"))

tchrist@convex.COM (Tom Christiansen) (02/18/91)

From the keyboard of nazgul@alphalpha.com (Kee Hinckley):
:It's clearly time to create alt.sources.showoff for people to post
:unneeded solutions to already solved problems.

I disagree.  I think seeing versions in shell, perl, icon, lisp, and now C
is good and healthy.  It shows how flexible UNIX is and exposes us to
idioms with which we may not all be familiar.  Think of it as an adventure
in comparitive cyberlinguistics.

On a somewhat unrelated note, can we change alt.sources to be called
comp.sources.free or comp.sources.any or something?  Not everyone gets it,
and it is quite valuable despite the occasional dross that shows up
there.

--tom
--
Tom Christiansen		tchrist@convex.com	convex!tchrist
 "All things are possible, but not all expedient."  (in life, UNIX, and perl)

tneff@bfmny0.BFM.COM (Tom Neff) (02/18/91)

In article <1991Feb17.021452.21206@alphalpha.com> nazgul@alphalpha.com (Kee Hinckley) writes:
>It's clearly time to create alt.sources.showoff for people to post
>unneeded solutions to already solved problems.
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Wouldn't we be violating a Unisys trademark?

kyle@UUNET.UU.NET (Kyle Jones) (02/18/91)

Kee Hinckley writes:
 > It's clearly time to create alt.sources.showoff for people to post
 > unneeded solutions to already solved problems.

David C Lawrence writes:
 > Speak for your own needs, bub.  reverse-region solved a problem for me
 > that the other solutions did not.  In my environment, it is much more
 > useful to me than any of the other reversal schemes I have seen as
 > part of this thread.

Amen.  The solution I posted was something I gave to a friend and
coworker around five years ago, on a system where there was no
Icon or Perl or tac.  Such systems still exist.  There's always
more than one way to solve a problem.  And since not everyone has
access to the same tools, this is fortunate.

A.C.G.Saunders@newcastle.ac.uk (Aidan Saunders) (02/19/91)

I missed the start of this thread, but if the subject line is still
relevant (:-)), how about using tail -r

ie:
 tail -r filename

or if it needs to be on the end of a pipe:

 ... | tail -r

Works OK in SunOS 4.1

Aidan

--
----------------------------------------------
ARPA :: a.c.g.saunders@newcastle.ac.uk
UUCP :: ...!ukc!newcastle.ac.uk!a.c.g.saunders
----------------------------------------------

guido@cwi.nl (Guido van Rossum) (02/21/91)

tchrist@convex.COM (Tom Christiansen) writes:

>I think seeing versions in shell, perl, icon, lisp, and now C
>is good and healthy.

And here's one in Python.  Judge for yourself.  It prints the result on stdout.

def reverse(filename):
	fp = open(filename, 'r')
	lines = []
	while 1:
		line = fp.readline()
		if not line: break
		lines.insert(0, line) # Insert in front of list
	for line in lines:
		print line,

If you want to reverse the file in place, replace the last two lines by:

	fp = open(filename, 'w')
	for line in lines:
		fp.write(line)

To make it into a script that reverses several files in place, add (to
the end, outside the function definition):

import sys
for filename in sys.argv[1:]:
	reverse(filename)

A more general function in Python to reverse the elements of any list
(actually it returns a reversed copy):

def revlist(list):
	result = []
	for item in list:
		result.insert(0, item)
	return result

This version uses 4*len(list) bytes of memory (if pointer size is 4
bytes) and can show quadratic behaviour (this depends on the
implementation of lists and in practice this is indeed the case).  It
could be improved by reversing smaller chunks and then concatenating
these but it would get hairier than I care about.  Reversing in place
would also be a possibility.

--Guido van Rossum <guido@cwi.nl> [disclaimer: all code above untested]

mario@cs.man.ac.uk (Mario Wolczko) (02/21/91)

> In <9102151917.AA04419@wendy-fate.UU.NET> kyle@UUNET.UU.NET (Kyle Jones):
> 
>    After seeing a couple of programs in Perl and Icon to do this, I
>    feel compelled to post a shell pipeline solution to this problem.
> 

Pass\'e solution, man.  Try this:
	ed - file <<\x
	g/^/m0
	w
	q
	x

Haven't you ever played "quiz function ed-command"? :-)

Mario Wolczko

PS Before anyone complains it's not a pipeline:
(cat >/tmp/pp$$ ; ed - /tmp/pp$$ <<\x ; cat /tmp/pp$$ ; rm /tmp/pp$$)
g/^/m0
w
q
x

   ______      Dept. of Computer Science   Internet:      mario@cs.man.ac.uk
 /~      ~\    The University              uucp:      mcsun!ukc!man.cs!mario
(    __    )   Manchester M13 9PL          JANET:         mario@uk.ac.man.cs
 `-':  :`-'    U.K.                        Tel: +44-61-275 6146  (FAX: 6280)
____;  ;_____________the mushroom project___________________________________

allbery@NCoast.ORG (Brandon S. Allbery KB8JRR) (02/26/91)

As quoted from <2166@m1.cs.man.ac.uk> by mario@cs.man.ac.uk (Mario Wolczko):
+---------------
| > In <9102151917.AA04419@wendy-fate.UU.NET> kyle@UUNET.UU.NET (Kyle Jones):
| >    After seeing a couple of programs in Perl and Icon to do this, I
| >    feel compelled to post a shell pipeline solution to this problem.
| 
| Pass\'e solution, man.  Try this:
| 	ed - file <<\x
| 	g/^/m0
| 	w
| 	q
| 	x
+---------------

I don't understand what's wrong with

	nl | sort -nr | cut -f2-

I use this about twice a month.  Scripts?  We don't need no steenking scripts!

++Brandon
-- 
Me: Brandon S. Allbery			    VHF/UHF: KB8JRR on 220, 2m, 440
Internet: allbery@NCoast.ORG		    Packet: KB8JRR @ WA8BXN
America OnLine: KB8JRR			    AMPR: KB8JRR.AmPR.ORG [44.70.4.88]
uunet!usenet.ins.cwru.edu!ncoast!allbery    Delphi: ALLBERY

chapin@cbnewsc.att.com ( Tom Chapin ) (02/27/91)

Brandon S. Allbery writes:
>I don't understand what's wrong with
>	nl | sort -nr | cut -f2-

NL doesn't automatically number blank lines, so a more bulletproof
version would be:

	nl -ba | sort +0nr -1 | cut -f2-

or one might even try:

	nl -ba | sort +0nr -1 | newform -l1 -b7


-- 
     tom chapin                att!hrccb!tjc         tjc@hrccb.att.com

vojta@powdermilk.berkeley.edu (Paul Vojta) (02/27/91)

In article <1991Feb26.025903.5850@NCoast.ORG> allbery@ncoast.ORG (Brandon S. Allbery KB8JRR) writes:
>
>I don't understand what's wrong with
>
>	nl | sort -nr | cut -f2-

What's wrong with

	tail -r

Or is that too simple?

--Paul Vojta, vojta@math.berkeley.edu

mario@cs.man.ac.uk (Mario Wolczko) (02/27/91)

In article <1991Feb19.132703.21495@newcastle.ac.uk>, A.C.G.Saunders@newcastle.ac.uk (Aidan Saunders) writes:
> 
> I missed the start of this thread, but if the subject line is still
> relevant (:-)), how about using tail -r
> 
> ie:
>  tail -r filename
> 
> or if it needs to be on the end of a pipe:
> 
>  ... | tail -r
> 
> Works OK in SunOS 4.1

Restriction: the maximum buffer size is 32Kb.  
Try tail -r file|tail -r|cmp - file on a long file.

Mario

   ______      Dept. of Computer Science   Internet:      mario@cs.man.ac.uk
 /~      ~\    The University              uucp:      mcsun!ukc!man.cs!mario
(    __    )   Manchester M13 9PL          JANET:         mario@uk.ac.man.cs
 `-':  :`-'    U.K.                        Tel: +44-61-275 6146  (FAX: 6280)
____;  ;_____________the mushroom project___________________________________

jfh@rpp386.cactus.org (John F Haugh II) (02/27/91)

In article <1991Feb27.010612.25618@agate.berkeley.edu> vojta@powdermilk.berkeley.edu (Paul Vojta) writes:
>What's wrong with
>
>	tail -r

% tail -r /etc/passwd
usage: tail [+|-[n][lbc][f]] [file]

>Or is that too simple?

Yup.  Watch out for those non-standard features.
-- 
John F. Haugh II                             UUCP: ...!cs.utexas.edu!rpp386!jfh
Ma Bell: (512) 832-8832                           Domain: jfh@rpp386.cactus.org
"I've never written a device driver, but I have written a device driver manual"
                -- Robert Hartman, IDE Corp.

net@opal.cs.tu-berlin.de (Oliver Laumann) (02/27/91)

In article <19074@rpp386.cactus.org> jfh@rpp386.cactus.org (John F Haugh II) writes:
> % tail -r /etc/passwd
> usage: tail [+|-[n][lbc][f]] [file]
> 
> [..] Watch out for those non-standard features.

Considering that the `tail' command under vanilla BSD (at least 4.2 and
4.3 BSD) has this `feature' I wouldn't call it non-standard.  After all,
`tail' is a BSD command.

--
Oliver Laumann    net@tub.cs.tu-berlin.de  net@tub.UUCP  net@pogo.ai.mit.edu

itkin@mrspoc.Transact.COM (Steven M. List) (02/28/91)

vojta@powdermilk.berkeley.edu (Paul Vojta) writes:

>In article <1991Feb26.025903.5850@NCoast.ORG> allbery@ncoast.ORG (Brandon S. Allbery KB8JRR) writes:
>>
>>I don't understand what's wrong with
>>
>>  nl | sort -nr | cut -f2-
>
>What's wrong with
>
>   tail -r
>
>Or is that too simple?
>
>--Paul Vojta, vojta@math.berkeley.edu

Berkeley BSD bigot!  %^}

Not all systems HAVE tail -r!!  Particularly not System V and derivatives.

But here's my favorite way to do it in ed/ex/vi:

    :g/./.m0

so if you're already in VI, then you just use this command.  You can
embed it as a HERE file in a shell script for ed, too.

Just for interest, a friend wrote an Emacs macro to reverse the characters
in each line.  I did the same in AWK:

    awk 'BEGIN  { new="" }
    { new=""; j = length
    for (i=length;i>0;i--) new=new substr($0,i,1)
    print new}'

If you combine the two, you have effectively reversed the entire file!
-- 
 +----------------------------------------------------------------------------+
 :                Steven List @ Transact Software, Inc. :^>~                  :
 :           Chairman, Unify User Group of Northern California                :
 :                         itkin@Transact.COM                                 :

jfh@rpp386.cactus.org (John F Haugh II) (02/28/91)

In article <2763@kraftbus.cs.tu-berlin.de> net@opal.cs.tu-berlin.de (Oliver Laumann) writes:
>In article <19074@rpp386.cactus.org> jfh@rpp386.cactus.org (John F Haugh II) writes:
>> % tail -r /etc/passwd
>> usage: tail [+|-[n][lbc][f]] [file]
>> 
>> [..] Watch out for those non-standard features.
>
>Considering that the `tail' command under vanilla BSD (at least 4.2 and
>4.3 BSD) has this `feature' I wouldn't call it non-standard.  After all,
>`tail' is a BSD command.

What would you prefer to call a feature which does not exist on all
systems that have the command?  What would you prefer to call a
command which may not exist on all systems?  How about ... a non-
standard feature on a non-standard command?

As for being a ``BSD'' feature, I've yet to see a UNIX system without
the command, and that includes my 9 year old XENIX system, and the 10
year old USG 5.0 system before that.  My ``Release 5.0 UNIX System
User's Manual'', which used to be dated July 1981 (or so, I forget)
gives the same 'tail [ +/-[number][lbc[f]]] [file]' usage I gave above.

The definitive test of BSD-ness was to drag out the 4.3-reno freed
sources tape and see if there was a tail there.  Nope, no tail.

Perhaps you have tail confused with head, which =is= a BSD command.
-- 
John F. Haugh II                             UUCP: ...!cs.utexas.edu!rpp386!jfh
Ma Bell: (512) 832-8832                           Domain: jfh@rpp386.cactus.org
"I've never written a device driver, but I have written a device driver manual"
                -- Robert Hartman, IDE Corp.

darcy@druid.uucp (D'Arcy J.M. Cain) (02/28/91)

In article <1991Feb27.010612.25618@agate.berkeley.edu> Paul Vojta writes:
>In article <1991Feb26.025903.5850@NCoast.ORG> allbery@ncoast.ORG (Brandon S. Allbery KB8JRR) writes:
>>I don't understand what's wrong with
>>	nl | sort -nr | cut -f2-
>What's wrong with
>	tail -r

It isn't available on all systems.

I once wrote a C program to do this.  Here is the code slightly modified
to remove some extra processing I needed for that application.  Note that
long lines could be handled using malloc/realloc but this worked for my
purposes at the time and it was fast.  It also didn't take more than 5
minutes to write, even with the extra stuff I needed at the time.

/* flip.c */
#include	<stdio.h>
void	flip(void)
{
	char	entry[128];

	if (gets(entry))
	{
		flip();
		printf("%s\n", entry);
	}
}

int		main(void)
{
	flip();
	return(0);
}

-- 
D'Arcy J.M. Cain (darcy@druid)     |
D'Arcy Cain Consulting             |   There's no government
West Hill, Ontario, Canada         |   like no government!
+1 416 281 6094                    |

net@opal.cs.tu-berlin.de (Oliver Laumann) (03/01/91)

In article <19079@rpp386.cactus.org> jfh@rpp386.cactus.org (John F Haugh II) writes:
> >Considering that the `tail' command under vanilla BSD (at least 4.2 and
> >4.3 BSD) has this `feature' I wouldn't call it non-standard.  After all,
> >`tail' is a BSD command.
> 
> What would you prefer to call a feature which does not exist on all
> systems that have the command? 

A vendor is free to put a version of `ls' on their UNIX port that
doesn't support the -l option any longer.  Does this make `ls -l'
non-standard?  Certainly not.

> What would you prefer to call a command which may not exist on all systems?

The fact that it may not exist on *all* systems is irrelevant.  I'm sure
that for almost any UNIX command (except maybe date, ls, etc.) you will
be able to find a system where this command does not exist.

> As for being a ``BSD'' feature, I've yet to see a UNIX system without
> the command,

Why do you think `tail' is under /usr/ucb (on those systems that have
a /usr/ucb)?  If the commands under /usr/ucb are not BSD commands,
then what *is* a BSD command?

--
Oliver Laumann    net@tub.cs.tu-berlin.de  net@tub.UUCP  net@pogo.ai.mit.edu

jfh@rpp386.cactus.org (John F Haugh II) (03/02/91)

In article <2775@kraftbus.cs.tu-berlin.de> net@opal.cs.tu-berlin.de (Oliver Laumann) writes:
>A vendor is free to put a version of `ls' on their UNIX port that
>doesn't support the -l option any longer.  Does this make `ls -l'
>non-standard?  Certainly not.

Name one that has done so.  This is a straw man argument.  No vendor
would ever remove the 'ls' command simply because removing it would
remove a significant part of what UNIX is.  However, the issue of
which options are ``standard'' in the 'tail' command is far more
clear - those options which the most common subset of implementations
contain.

As I pointed out from the Release 5.0 UNIX System User's Manual, the
`-r' option was not a ``common'' feature in System V UNIX.  This manual
predates 4.3BSD many several years.  System V UNIX is the most common
UNIX running on i286 and i386 systems, which are themselves the most
common PC platforms running UNIX.  And in the one-up category, I know
that 'tail' was present in the USG 3.0 and 4.0 releases for the PDP-11
since I used it when tailing logs from kernel builds.

>The fact that it may not exist on *all* systems is irrelevant.  I'm sure
>that for almost any UNIX command (except maybe date, ls, etc.) you will
>be able to find a system where this command does not exist.

No, but the fact that it does not exist on =most= systems is relevant.
The tail command exists on every single UNIX system I've ever used, and
that numbers over a dozen.  But the '-r' option exists where?  [ Yeah, I'm
a System V bigot, so don't point out that it exists in BSD land ... ]

>> As for being a ``BSD'' feature, I've yet to see a UNIX system without
>> the command,
>
>Why do you think `tail' is under /usr/ucb (on those systems that have
>a /usr/ucb)?  If the commands under /usr/ucb are not BSD commands,
>then what *is* a BSD command?

How about ... it's there because it contains BSD-only features?  Is it
possible that -r is a BSD-only feature and they put the command there
because of that?

If tail is a BSD-only command why isn't the source part of the freed
BSD source code?  It isn't.  I'm certain someone with a V7 manual
(mine has left my possession years ago) will verify my statement that
it was a part of 7th Edition or System III at the least.  I do seem
to recall seeing tail used in examples given in documents written about
the time of V7 (which predates all the 4BSD releases ...)

I was going to get my German/English dictionary out and try to explain
it to you in German, but your English seems good enough.  What part of
"-r isn't a standard feature" don't you understand exactly?
-- 
John F. Haugh II        | Distribution to  | UUCP: ...!cs.utexas.edu!rpp386!jfh
Ma Bell: (512) 832-8832 | GEnie PROHIBITED :-) |  Domain: jfh@rpp386.cactus.org
"I've never written a device driver, but I have written a device driver manual"
                -- Robert Hartman, IDE Corp.

ned@pebbles.cad.mcc.com (Ned Nowotny) (03/02/91)

In article <2775@kraftbus.cs.tu-berlin.de> net@opal.cs.tu-berlin.de (Oliver Laumann) writes:
=>In article <19079@rpp386.cactus.org> jfh@rpp386.cactus.org (John F Haugh II) writes:
=>> >Considering that the `tail' command under vanilla BSD (at least 4.2 and
=>> >4.3 BSD) has this `feature' I wouldn't call it non-standard.  After all,
=>> >`tail' is a BSD command.
=>> 
=>> What would you prefer to call a feature which does not exist on all
=>> systems that have the command? 
=>
=>A vendor is free to put a version of `ls' on their UNIX port that
=>doesn't support the -l option any longer.  Does this make `ls -l'
=>non-standard?  Certainly not.
=>
=>> What would you prefer to call a command which may not exist on all systems?
=>
=>The fact that it may not exist on *all* systems is irrelevant.  I'm sure
=>that for almost any UNIX command (except maybe date, ls, etc.) you will
=>be able to find a system where this command does not exist.
=>
=>> As for being a ``BSD'' feature, I've yet to see a UNIX system without
=>> the command,
=>
=>Why do you think `tail' is under /usr/ucb (on those systems that have
=>a /usr/ucb)?  If the commands under /usr/ucb are not BSD commands,
=>then what *is* a BSD command?

I think you might find it there because that version of tail is a BSD version
which supports the -r flag.  However, that does not make tail a BSD command.
In fact, tail has been a standard UNIX command in every flavor of UNIX of which
I am aware since at least version 7.  To quote the 7th edition manual set:

NAME
	tail - deliver the last part of a file
SYNOPSIS
	tail [ +/-number[lbc] ] [ file ]
.
.
.

Ned Nowotny, MCC CAD Program, Box 200195, Austin, TX  78720  Ph: (512) 338-3715
ARPA: ned@mcc.com                   UUCP: ...!cs.utexas.edu!milano!cadillac!ned
-------------------------------------------------------------------------------
"We have ways to make you scream." - Intel advertisement in the June 1989 DDJ.

iisakkil@niksula.hut.fi (Mika R Iisakkila) (03/02/91)

	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...

tchrist@convex.COM (Tom Christiansen) (03/03/91)

From the keyboard of iisakkil@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@convex.com      convex!tchrist