Operating System - Linux
1752766 Members
5204 Online
108789 Solutions
New Discussion юеВ

Re: How to print certain pages from a disk file

 
SOLVED
Go to solution
Hoey
Occasional Advisor

How to print certain pages from a disk file

Hi all

I want to print a range of pages from a disk file (a lengthly report). As I need to pass 3 arguments to this script, so the syntax will be "prnpage.sh filename from_page to_page". The pages are not fixed length because of control breaks. (eg. print by sales rep by state etc). On the report, there is a control code "^L" to denote new page.

I managed to print a range of pages successfully if number of detail lines on pages are the same.

Also, how can I grep the "^L"?

Any idea to tackle this problem is appreciated.

Thanks in advance.

Davis
3 REPLIES 3
Ermin Borovac
Honored Contributor

Re: How to print certain pages from a disk file

You can try using software called a2ps.

$ a2ps --interpret=yes --pages=1-3 --output test.ps test.txt

--interpret=yes makes it interpret form feed character (^L) as page break
--pages to select what pages to print
--output for testing then examine test.ps to make sure it's what you expect

You can download a2ps from

http://hpux.connect.org.uk

or the closest mirror
Stephen Keane
Honored Contributor
Solution

Re: How to print certain pages from a disk file

To get the ^L search pattern type Quote CTRL-V CTRL-L Quote. i.e. hold down the CTRL key and press V then L without letting go of the control key.

A basic script could be something like ...


FILE=$1
FP=$2
LP=$3

if [ "$FP" -eq 1 ]
then
FL=0
else
FP=`expr $FP - 1`
FL=`grep -n "^L" $1 | cut -f1 -d":" | head -$FP | tail -1`
fi

TP=`grep -n "^L" $1 | wc -l`

if [ "$LP" -gt "$TP" ]
then
LL=`wc -l $1 | awk '{ print $1 }'`
else
LL=`grep -n "^L" $1 | cut -f1 -d":" | head -$LP | tail -1`
fi

TL=`expr $LL - $FL`

head -$LL $1 | tail -$TL


Where TP=total pages, TL=total lines, FP=first page, FL=first line, LP=last page, LL=last line. You might have to change it depending on whether the last page in the file has a ^L at the end of it or not. Also what do you want to do if they request page 6 of a 5 page document? The script above would print the last page in the document in response.
Hoey
Occasional Advisor

Re: How to print certain pages from a disk file

Thanks to Ermin & Stephen. My scripts are working perfectly now.