home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / gawk-2.15.6-base.tgz / gawk-2.15.6-base.tar / fsf / gawk / test / lastnpages < prev    next >
Internet Message Format  |  1993-10-19  |  2KB

  1. From nstn.ns.ca!news.cs.indiana.edu!news.nd.edu!spool.mu.edu!uunet!elroy.jpl.nasa.gov!swrinde!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!dali.cs.montana.edu!milton!uw-beaver!fluke!ssc-vax!brennan Mon May  6 23:41:40 ADT 1991
  2. Article: 26492 of comp.unix.questions
  3. Path: cs.dal.ca!nstn.ns.ca!news.cs.indiana.edu!news.nd.edu!spool.mu.edu!uunet!elroy.jpl.nasa.gov!swrinde!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!dali.cs.montana.edu!milton!uw-beaver!fluke!ssc-vax!brennan
  4. From: brennan@ssc-vax.UUCP (Michael D Brennan)
  5. Newsgroups: comp.unix.questions
  6. Subject: Re: How to print last <n> pages of a file
  7. Message-ID: <3948@ssc-bee.ssc-vax.UUCP>
  8. Date: 6 May 91 15:42:00 GMT
  9. Article-I.D.: ssc-bee.3948
  10. Organization: Boeing Aerospace & Electronics, Seattle WA
  11. Lines: 33
  12.  
  13.  
  14. The following shell & (new) awk program prints the last n pages.
  15.  
  16. If you get more than 65 lines to a page, the program that inserts
  17. the ^L's should be fixed.
  18.  
  19. -------------------------------------------------------------
  20. #!/bin/sh
  21. # usage: lastpages   --  prints 1 page reads stdin
  22. #        lastpages  n -- prints  n pages reads stdin
  23. #        lastpages  n  files -- prints n pages, reads file list
  24.  
  25. program='BEGIN{RS = ORS = "\f" }
  26.  
  27.  
  28. { page[NR] = $0 
  29.   if ( NR > numpages )  delete  page[NR-numpages]
  30. }
  31.  
  32. END { 
  33.   i = NR - numpages + 1
  34.   if ( i <= 0 ) i = 1
  35.  
  36.   while( i <= NR )  print page[i++]
  37. }'
  38.  
  39.  
  40. case $# in 
  41. 0)  awk "$program" numpages=1 - ;;
  42. 1)  awk "$program" numpages=$1 - ;;
  43. *)  pages=$1 ; shift
  44.     awk "$program" numpages=$pages  $* ;;
  45. esac
  46.  
  47.  
  48.