[comp.unix.questions] How to print last <n> pages of a file?

pjs@euclid.jpl.nasa.gov (Peter Scott) (05/02/91)

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
brain on news.  Any questions?    |    (pjs@euclid.jpl.nasa.gov)

merlyn@iwarp.intel.com (Randal L. Schwartz) (05/03/91)

In article <1991May2.162355.779@elroy.jpl.nasa.gov>, pjs@euclid (Peter Scott) writes:
| 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 (<>) {
	push(@q,$_);
	shift(@q) while @q > $tail; # keep only $tail pages in queue
}
print @q;
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      |
| merlyn@iwarp.intel.com ...!any-MX-mailer-like-uunet!iwarp.intel.com!merlyn |
\=Cute Quote: "Intel: putting the 'backward' in 'backward compatible'..."====/

rhartman@thestepchild.sgi.com (Robert Hartman) (05/03/91)

In article <1991May2.162355.779@elroy.jpl.nasa.gov> pjs@euclid.jpl.nasa.gov writes:
>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

pjs@euclid.jpl.nasa.gov (Peter Scott) (05/03/91)

In article <1991May2.182639.21845@iwarp.intel.com>, merlyn@iwarp.intel.com (Randal L. Schwartz) writes:
> In article <1991May2.162355.779@elroy.jpl.nasa.gov>, pjs@euclid (Peter Scott) writes:
> | 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 (<>) {
> 	push(@q,$_);
> 	shift(@q) while @q > $tail; # keep only $tail pages in queue
> }
> print @q;
> 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
brain on news.  Any questions?    |    (pjs@euclid.jpl.nasa.gov)

lwall@jpl-devvax.jpl.nasa.gov (Larry Wall) (05/04/91)

In article <1991May3.002457.18028@elroy.jpl.nasa.gov> pjs@euclid.jpl.nasa.gov writes:
: In article <1991May2.182639.21845@iwarp.intel.com>, merlyn@iwarp.intel.com (Randal L. Schwartz) writes:
: > In article <1991May2.162355.779@elroy.jpl.nasa.gov>, pjs@euclid (Peter Scott) writes:
: > | 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 (<>) {
: > 	push(@q,$_);
: > 	shift(@q) while @q > $tail; # keep only $tail pages in queue
: > }
: > print @q;
: > 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 (<>) {
    push(@q, $1) while s/^((.*\n){66})//;
    push(@q, $_) if length;
    shift(@q) while @q > $tail;
}
print @q;

Larry Wall
lwall@netlabs.com