[comp.sources.wanted] Backwards cat

odin@ucscb.UCSC.EDU (Jon Granrose) (12/15/89)

I am looking for a program that will cat a file backwards.  Not with all the
letters reversed but one that prints the last line, then the second to last
line, 3 from the last line, etc.)  If it doesn't exist then I will write my
own but I thought I'd ask first.

Thanks

Jon
-- 
 _____________________________________________________________________________
|Jon Granrose        |ARPA: odin@ucscb.UCSC.EDU   jonathan@sco.com |  // Only |
|Cowell College, UCSC|      74036.3241@compuserve.com              |\X/ Amiga!|
|Santa Cruz, CA 95064|UUCP:..!ucbvax!ucscc!ucscb!odin Bitnet:odin@ucscb.bitnet|
|      "Remember!  No matter where you go, there you are." - B. Banzai        |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

falk@peregrine.Sun.COM (Ed Falk) (12/16/89)

>
>I am looking for a program that will cat a file backwards.  Not with all the
>letters reversed but one that prints the last line, then the second to last
>line, 3 from the last line, etc.)  If it doesn't exist then I will write my
>own but I thought I'd ask first.

The unix tail(1) command will do it.

karl@MorningStar.Com (Karl Fox) (12/16/89)

In article <6488@lindy.Stanford.EDU> odin@ucscb.UCSC.EDU (Jon Granrose) writes:

   I am looking for a program that will cat a file backwards.  Not with all the
   letters reversed but one that prints the last line, then the second to last
   line, 3 from the last line, etc.)  If it doesn't exist then I will write my
   own but I thought I'd ask first.

Try this:

    #!/bin/sh
    grep -n "^" $* | sort -rn -t: | cut -d: -f2-
--
Karl Fox, Morning Star Technologies               karl@MorningStar.COM

bill@twwells.com (T. William Wells) (12/16/89)

In article <6488@lindy.Stanford.EDU> odin@ucscb.UCSC.EDU (Jon Granrose) writes:
: I am looking for a program that will cat a file backwards.  Not with all the
: letters reversed but one that prints the last line, then the second to last
: line, 3 from the last line, etc.)  If it doesn't exist then I will write my
: own but I thought I'd ask first.

Try: nl | sort -nr | cut (you figure the arguments). I would not
be surprised to find that this is as fast as a "tailored" program.
(Why? Because the disk access pattern of the sort *might* be
better than that of a file reverser.)

---
Bill                    { uunet | novavax | ankh | sunvice } !twwells!bill
bill@twwells.com

" Maynard) (12/16/89)

In article <6488@lindy.Stanford.EDU> odin@ucscb.UCSC.EDU (Jon Granrose) writes:
>I am looking for a program that will cat a file backwards.  Not with all the
>letters reversed but one that prints the last line, then the second to last
>line, 3 from the last line, etc.)  If it doesn't exist then I will write my
>own but I thought I'd ask first.

It's probably pretty simple to do in C, but here's a quick and dirty in
awk I use to insure that my disks get unmounted in the reverse order of
being mounted:

awk '{ lines[NR] = $0 } END { for(i=NR; i>=1; i--) print lines[i]; }' <file

It's not the fastest in the world, and it's limited by awk's available
memory, but it does the job...

-- 
Jay Maynard, EMT-P, K5ZC, PP-ASEL   | Never ascribe to malice that which can
jay@splut.conmicro.com       (eieio)| adequately be explained by stupidity.
{attctc,bellcore}!texbell!splut!jay +----------------------------------------
     Here come Democrats...here come Democrats...throwing money a-way...

merlyn@iwarp.intel.com (Randal Schwartz) (12/16/89)

In article <6488@lindy.Stanford.EDU>, odin@ucscb (Jon Granrose) writes:
| I am looking for a program that will cat a file backwards.  Not with all the
| letters reversed but one that prints the last line, then the second to last
| line, 3 from the last line, etc.)  If it doesn't exist then I will write my
| own but I thought I'd ask first.

Don't write your own... get Perl...

perl -e 'print reverse(<>);'

Simple, eh?

Just another Perl hacker,
-- 
/== Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ====\
| on contract to Intel's iWarp project, Hillsboro, Oregon, USA, Sol III  |
| merlyn@iwarp.intel.com ...!uunet!iwarp.intel.com!merlyn	         |
\== Cute Quote: "Welcome to Oregon... Home of the California Raisins!" ==/

irwin@m.cs.uiuc.edu (12/16/89)

/* Written  2:12 am  Dec 15, 1989 by odin@ucscb.UCSC.EDU in m.cs.uiuc.edu:comp.sources.wanted */
/* ---------- "Backwards cat" ---------- */


>I am looking for a program that will cat a file backwards.  Not with all the
>letters reversed but one that prints the last line, then the second to last
>line, 3 from the last line, etc.)  If it doesn't exist then I will write my
>own but I thought I'd ask first.

>Thanks

>Jon
-- 
 _____________________________________________________________________________
|Jon Granrose        |ARPA: odin@ucscb.UCSC.EDU   jonathan@sco.com |  // Only |
|Cowell College, UCSC|      74036.3241@compuserve.com              |\X/ Amiga!|
|Santa Cruz, CA 95064|UUCP:..!ucbvax!ucscc!ucscb!odin Bitnet:odin@ucscb.bitnet|
|      "Remember!  No matter where you go, there you are." - B. Banzai        |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* End of text from m.cs.uiuc.edu:comp.sources.wanted */

It sounds like you could use the LESS utility to do what you want. It
is mentioned in this forum in another thread.

Al Irwin
Univ of Illinois
Dept of Comp Sci
irwin@m.cs.uiuc.edu

tomm@voodoo.UUCP (Tom Mackey) (12/16/89)

In article <6488@lindy.Stanford.EDU> odin@ucscb.UCSC.EDU (Jon Granrose) writes:


>I am looking for a program that will cat a file backwards.  Not with all the
>letters reversed but one that prints the last line, then the second to last
>line, 3 from the last line, etc.)  If it doesn't exist then I will write my
>own but I thought I'd ask first.

>Thanks

>Jon
>-- 
> _____________________________________________________________________________
>|Jon Granrose        |ARPA: odin@ucscb.UCSC.EDU   jonathan@sco.com |  // Only |
>|Cowell College, UCSC|      74036.3241@compuserve.com              |\X/ Amiga!|
>|Santa Cruz, CA 95064|UUCP:..!ucbvax!ucscc!ucscb!odin Bitnet:odin@ucscb.bitnet|
>|      "Remember!  No matter where you go, there you are." - B. Banzai        |
>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


I am going to post the source and manpage for tac to alt.sources.  I suspect
that many will send you the source, but I haven't seen it posted lately.

If you don't get alt.sources, and still want it, let me know and I'll try
exercising our mailer.

-- 
Tom Mackey                                (206) 234-7767 (wk)
Boeing Computer Services    ....uw-beaver!ssc-vax!voodoo!tomm
M/S 6M-17,     P.O. Box 24346,     Seattle, WA     98124-0346

6600pete@hub.UUCP (12/16/89)

From article <6488@lindy.Stanford.EDU>, by odin@ucscb.UCSC.EDU (Jon Granrose):
> I am looking for a program that will cat a file backwards.  Not with all the
> letters reversed but one that prints the last line, then the second to last
> line, 3 from the last line, etc.)  If it doesn't exist then I will write my
> own but I thought I'd ask first.

...and the fourth answer...

TAIL(1)                  USER COMMANDS                    TAIL(1)

NAME
     tail - display the last part of a file

SYNOPSIS
     tail +|-number [ lbc ] [ fr ] [ filename ]

OPTIONS
     r    Copy lines from the end of the file in  reverse  order.
          The  default  for  r  is  to  print  the entire file in
          reverse order.
-------------------------------------------------------------------------------
Pete Gontier   | InterNet: 6600pete@ucsbuxa.ucsb.edu, BitNet: 6600pete@ucsbuxa
Editor, Macker | Online Macintosh Programming Journal; mail for subscription
Hire this kid  | Mac, DOS, C, Pascal, asm, excellent communication skills

morfeq@boulder.Colorado.EDU (Ali H. Morfeq) (12/16/89)

there is procedure written in C in the book :

           "Advanced Unix programming' by Marc J. Rochkind, Prentice-Hall
           1985.

It is in section 2.11. it is exactley what you described. It takes
a file name and output the file in reverse order of lines.
 for example:  if file : dog
                         bites
                         man
 then it will produce:
                        man
                        bites
                        dog


morfeq@boulder.colorado.edu

ken@cs.rochester.edu (Ken Yap) (12/16/89)

So does a backwards cat go "woaim"? Sorry, couldn't resist. :-)

roy@comcon.UUCP (Roy M. Silvernail) (12/16/89)

In article <3324@hub.UUCP>, 6600pete@hub.UUCP writes:
> From article <6488@lindy.Stanford.EDU>, by odin@ucscb.UCSC.EDU (Jon Granrose):
> > I am looking for a program that will cat a file backwards.  Not with all the
> > letters reversed but one that prints the last line, then the second to last
> > line, 3 from the last line, etc.)  If it doesn't exist then I will write my
> > own but I thought I'd ask first.
> 
> ...and the fourth answer...
> 
> TAIL(1)                  USER COMMANDS                    TAIL(1)
> 

Hmmm... at this site, not quite:

tail -r data
usage: tail [+/-[n][lbc][f]] [file]

man tail
man: tail not found

ah, well.... I don't have to reverse files very often:-)

-- 
_R_o_y _M_. _S_i_l_v_e_r_n_a_i_l  | UUCP: uunet!comcon!roy  |  "No, I don't live in an igloo!"
[ah, but it's my account... of course I opine!]           -Sourdough's riposte
SnailMail: P.O. Box 210856, Anchorage, Alaska, 99521-0856, U.S.A., Earth, etc.

tchrist@convex.COM (Tom Christiansen) (12/17/89)

In article <5360@omepd.UUCP> merlyn@iwarp.intel.com (Randal Schwartz) writes:
|In article <6488@lindy.Stanford.EDU>, odin@ucscb (Jon Granrose) writes:
|| I am looking for a program that will cat a file backwards.  Not with all the
|| letters reversed but one that prints the last line, then the second to last
|| line, 3 from the last line, etc.)  If it doesn't exist then I will write my
|| own but I thought I'd ask first.
|
|Don't write your own... get Perl...
|perl -e 'print reverse(<>);'
|Simple, eh?

My first reaction at the original posting was to use "tail -r", but I like
Randal's solution a good deal anyway.  You'd think a dedicated utility
like tail would be more efficient than a general purpose interpreted
language like perl (or awk, which is less general-purpose), but much to my
surprise, I found this (on a minimally configured CONVEX C1):

% wc /etc/termcap
    2235    8179  102598 /etc/termcap
% time tail -r /etc/termcap > /dev/null
1.4u 0.3s 0:01 93% 0+18k 0+0io 67pf+0w
% time perl -e 'print reverse(<>);' /etc/termcap > /dev/null
0.7u 0.3s 0:01 91% 0+25k 0+5io 127pf+0w

I also tried Jay Maynards awk solution:

% time awk '{ lines[NR] = $0 } END { for(i=NR; i>=1; i--) print lines[i]; }' < /etc/termcap > /dev/null
9.3u 0.9s 0:12 79% 0+3k 27+0io 95pf+0w
% time nawk ....
6.9u 0.8s 0:08 89% 0+4k 39+1io 114pf+0w
% time gawk ....
6.6u 1.9s 0:09 86% 0+4k 11+8io 214pf+0w


Which is pretty sad performance by my book.

--tom

    Tom Christiansen                       {uunet,uiucdcs,sun}!convex!tchrist 
    Convex Computer Corporation                            tchrist@convex.COM
		 "EMACS belongs in <sys/errno.h>: Editor too big!"

emv@math.lsa.umich.edu (Edward Vielmetti) (12/17/89)

I mailed this to Jon but I haven't seen it here yet.

'tac', from 4.3 BSD contrib, author is Jay Lepreau.

didn't we have this discussion a few weeks ago?  Perhaps
this is something for 'Frequently Answered Questions.'.

--Ed

guy@auspex.UUCP (Guy Harris) (12/19/89)

>...and the fourth answer...

Which isn't correct:

	auspex% tail -r /etc/termcap > /tmp/stuff
	auspex% ls -lL /tmp/stuff /etc/termcap
	-rw-rw-r--  1 root       133962 Jul 14 14:39 /etc/termcap
	-rw-r--r--  1 guy         32768 Dec 18 13:01 /tmp/stuff

32768 != 133962.  "tail -r" doesn't reverse the *entire* file, just a
chunk at the end, in the UNIX versions I know of:

	BUGS
	     Data for a tail relative to the end of the file is stored in
	     a buffer, and thus is limited in size.