How to print last <n> pages of a file?

How to print last <n> pages of a file?

Post by Peter Sco » Sat, 04 May 1991 01:23:55



How can I print the last <n> pages of a text file?  Said file has form
feeds at random places, so "tail -132 file | lpr" won't work.  Now, I tried
"pr file | tail -132" but that includes the 5-line headers and footers
that I don't want and don't get with "lpr file".  So I tried
"pr -t file | tail -132" but the -t option turns off page filling so the
form feeds aren't expanded :-(.

Any suggestions?  SunOS 4.1 over here.

--
This is news.  This is your       |    Peter Scott, NASA/JPL/Caltech

 
 
 

How to print last <n> pages of a file?

Post by Randal L. Schwar » Sat, 04 May 1991 03:26:39


| How can I print the last <n> pages of a text file?  Said file has form
| feeds at random places, so "tail -132 file | lpr" won't work.  Now, I tried
| "pr file | tail -132" but that includes the 5-line headers and footers
| that I don't want and don't get with "lpr file".  So I tried
| "pr -t file | tail -132" but the -t option turns off page filling so the
| form feeds aren't expanded :-(.
|
| Any suggestions?  SunOS 4.1 over here.

A quick Perl solution:

################################################## snip
#!/usr/bin/perl

$tail = shift || 1;
        ## first arg is number of pages to tail (default is 1)
        ## rest of args are processed like "cat"

$/ = "\f"; # input separator set to form-feed
while (<>) {


Quote:}


exit 0;
################################################## snip

Perl is available for free for all UNIX-like os's at any of the GNU
sites or devvax.jpl.nasa.gov.

print "Just another Perl hacker,"
--
/=Randal L. Schwartz, Stonehenge Consulting Services (503)777-0095 ==========\
| on contract to Intel's iWarp project, Beaverton, Oregon, USA, Sol III      |

\=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/

 
 
 

How to print last <n> pages of a file?

Post by Peter Sco » Sat, 04 May 1991 09:24:57




> | How can I print the last <n> pages of a text file?  Said file has form
> | feeds at random places, so "tail -132 file | lpr" won't work.  Now, I tried
> | "pr file | tail -132" but that includes the 5-line headers and footers
> | that I don't want and don't get with "lpr file".  So I tried
> | "pr -t file | tail -132" but the -t option turns off page filling so the
> | form feeds aren't expanded :-(.
> |
> | Any suggestions?  SunOS 4.1 over here.

> A quick Perl solution:

> ################################################## snip
> #!/usr/bin/perl

> $tail = shift || 1;
>    ## first arg is number of pages to tail (default is 1)
>    ## rest of args are processed like "cat"

> $/ = "\f"; # input separator set to form-feed
> while (<>) {


> }

> exit 0;
> ################################################## snip

Hmm, nice, but tell me, does it work when a page *doesn't* have a form
feed to separate it?  I.e., there's more than 66 lines between FF's?

BTW, I should have said "<n>*66" instead of "132" in my posting.  Obviously
my immediate need is to print 2 pages...

--
This is news.  This is your       |    Peter Scott, NASA/JPL/Caltech

 
 
 

How to print last <n> pages of a file?

Post by Robert Hartm » Sat, 04 May 1991 07:57:50



>How can I print the last <n> pages of a text file?  Said file has form
>feeds at random places, so "tail -132 file | lpr" won't work.  Now, I tried
>"pr file | tail -132" but that includes the 5-line headers and footers
>that I don't want and don't get with "lpr file".  So I tried
>"pr -t file | tail -132" but the -t option turns off page filling so the
>form feeds aren't expanded :-(.

>Any suggestions?  SunOS 4.1 over here.

Sounds like a job for csplit to me!  Split the pr output at the ^L characters,
and take the last n resulting files!

-r

 
 
 

How to print last <n> pages of a file?

Post by Larry Wa » Sun, 05 May 1991 03:32:51




: > | How can I print the last <n> pages of a text file?  Said file has form
: > | feeds at random places, so "tail -132 file | lpr" won't work.  Now, I tried
: > | "pr file | tail -132" but that includes the 5-line headers and footers
: > | that I don't want and don't get with "lpr file".  So I tried
: > | "pr -t file | tail -132" but the -t option turns off page filling so the
: > | form feeds aren't expanded :-(.
: > |
: > | Any suggestions?  SunOS 4.1 over here.
: >
: > A quick Perl solution:
: >
: > ################################################## snip
: > #!/usr/bin/perl
: >
: > $tail = shift || 1;
: >  ## first arg is number of pages to tail (default is 1)
: >  ## rest of args are processed like "cat"
: >
: > $/ = "\f"; # input separator set to form-feed
: > while (<>) {


: > }

: > exit 0;
: > ################################################## snip
:
: Hmm, nice, but tell me, does it work when a page *doesn't* have a form
: feed to separate it?  I.e., there's more than 66 lines between FF's?

OK, change that to:

#!/usr/bin/perl

$tail = shift || 1;

$/ = "\f"; # input separator set to form-feed
while (<>) {



Quote:}


Larry Wall

 
 
 

How to print last <n> pages of a file?

Post by Brian Ri » Sat, 04 May 1991 11:04:29


:-<words>  

That is a picture of a person (me) eating his words.

In the referred-to posting, a Perl aficionado gives a Perl solution
to a problem (printing the last n pages of a file), along with a
perfectly gracious plug for Perl.  Now, for assorted ungracious
reasons, I have not been in the Perl camp; I felt that...

    (a) Perl code is too cryptic;
    (b) One can write a solution to any old problem using
        standard tools in less time than it takes to retrieve
        and compile Perl; and
    (c) Perl, being a kitchen-sink kinda thing, is just sorta
        inelegant.

So when I saw the Perl posting, I thought, "Hah!  I'll just knock
off a solution using awk...won't take a second, especially given
awk's capacity to use a given character other than space as a
field separator."

Well, that was some time ago.  Quite some time ago indeed.  
There goes (b).  I'm giving up; it's pretty tricky.  If you'd like
to try, remember that you have to deal with input files like this:

    This is page 1.
    More text on page 1.
    ^L
    This is page 2.
    ^LThis is page 3.^L
    ^LThis is page 5 (yes, I do mean 5).
    More text on page 5.^LThis is page 6.
    More text on page 6.^LThis is page 7.^L
    ^LThis is page 9.^LThis is page 10.^L

My code is a rat's nest, and it's looking like it'll get uglier
the closer it gets to being correct (not that it's going to
get any closer).  Bye-bye, (a).  And it occurred to me
as I was toiling that awk is kind of a kitchen-sink thing too,
except we're used to it.  Bye-bye, (c).  If anyone has an awk
solution which can handle the above text file and is elegant
and clearly understood, I'd like to see it.  Yer a tougher
dude than I.  (Don't forget to handle the case where you go
to a new page because you had more than 65 lines on the previous
one; admittedly, that's the easy part, but we gotta be
complete.  And the Perl poster seems to have forgotten
that case in his [admittedly justifiable] enthusiasm :-).)

So maybe I'll be learning Perl soon.  But I do have one final
malediction: I really think that the public competitions we've
seen among Perl devotees to find maximally compact solutions
to coding problems are ill-advised because (if you'll pardon
the overweening moralism) they set a bad example for new
programmers.  There's nothing like time spent maintaining
other people's code to change yer perspective on that sort
of high-jinks...  But I'm just a killjoy, anyway.  Have fun,
y'all.
--

DG/UX Product Assurance Engineering
Data General Corp., Research Triangle Park, N.C.
"Boy, I hope those dogs eat that cat." --Tula, age 3

 
 
 

1. How to print last <n> pages of a file

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

2. dip/slip DISPLAY

3. I need a example of X .twmrc file PLEASE <<<<<<<<<<<<<<<<<<<<<

4. Diamond fixes archive Web site

5. Latest 1.<even>.<n>

6. Mandriva 2006_64: Won't recognize local printer

7. <<<<<soundconfig problem <<<<<<<

8. Can one timeslave to Solaris 2.x?

9. How to print from last page to first page?

10. SOLARIS 2.3 NOT getting the NAMESERVER <<<<<<<<<<<<<<<<<<<<

11. not printing last page of long print outs.

12. Script for makeing whole bunch of account of sol. 2.3 <<<<<<<<<<<<<<<<<

13. CDE <<<<<<<<<-------------- Need Help !