How to print last <n> pages of a file

Michael D Brennan brennan at ssc-vax.UUCP
Tue May 7 01:42:00 AEST 1991


The following shell & (new) awk program prints the last n pages.

If you get more than 65 lines to a page, the program that inserts
the ^L's should be fixed.

-------------------------------------------------------------
#!/bin/sh
# usage: lastpages   --  prints 1 page reads stdin
#        lastpages  n -- prints  n pages reads stdin
#        lastpages  n  files -- prints n pages, reads file list

program='BEGIN{RS = ORS = "\f" }


{ page[NR] = $0 
  if ( NR > numpages )  delete  page[NR-numpages]
}

END { 
  i = NR - numpages + 1
  if ( i <= 0 ) i = 1

  while( i <= NR )  print page[i++]
}'


case $# in 
0)  awk "$program" numpages=1 - ;;
1)  awk "$program" numpages=$1 - ;;
*)  pages=$1 ; shift
    awk "$program" numpages=$pages  $* ;;
esac



More information about the Comp.unix.questions mailing list