[alt.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!" ==/

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

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.

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.