Operating System - HP-UX
1834142 Members
2328 Online
110064 Solutions
New Discussion

Print text per page number

 
Joseph Bague
Frequent Advisor

Print text per page number

Hi all,

I have a huge file on HPUX containing a report. The file contain like this:

Page 1
text
text
text
Page 2
text
text
text
Page 3
text
text
text
Page 4
text
and so on

I want to print it by particular pages. ie from page 1 to 3 or from page 2 to 4. How can I do this ?

Thanks in advance,
Joseph
Expect nothing but ready for everything
11 REPLIES 11
Rajeev  Shukla
Honored Contributor

Re: Print text per page number

One way you can do is yank those pages(lines and save them in another file and then print off that file.
Like open the fine in vi then
:50,100 w file1

you'll get a file1 with those 50-100 lines and then print that file1 and later on delete it.

Carlos Fernandez Riera
Honored Contributor

Re: Print text per page number

awk ' /page 2/,/page 4/ { print }' file
unsupported
Joseph Bague
Frequent Advisor

Re: Print text per page number

Hi Rajeev

My file size is 1GB so I am afraid to open it with vi. It's actually a oracle report that already submitted to print job, Unfortunately during printing the printer failed. So instead to run it on oracle and reprint it again I decided to print it on OS side. I want to start the print with the page where printing cut off.

Carlos

I tried your command but I didn't see any output.
Expect nothing but ready for everything
Leif Halvarsson_2
Honored Contributor

Re: Print text per page number

Hi,
Perhaps you can use "split":
"split -l 1000" filename" splits the file into pieces with 1000 lines in each. Then you can print out any of the pieces you want.
Bill Hassell
Honored Contributor

Re: Print text per page number

The filesize is far too large for vi. The awk comand will work as long as the string match is exact and this includes extra spaces between the word Page and the number. I would extract the first few hundred lines (head -200 bigfilename) and use it to test your extract method.


Bill Hassell, sysadmin
john korterman
Honored Contributor

Re: Print text per page number

Hi Joseph,
if you only want to print the lines from where printing stopped, you can perhaps make use of the tail command:
First, find a distinctive string in the last printed line and retrieve its line number, e.g.:
# grep -n "distinctive string"
which should return :. If you are sure you have got the correct string, then calculate the total number of lines in your file:
# wc -l


Now calculate the position of the last printed line from the end:
# echo - | bc


Try printing the rest with tail, e.g.:
# tail -

Unfortunately, it might fail because of the limitations of tail and the size of your file.

regards,
John K.
it would be nice if you always got a second chance
Pete Randall
Outstanding Contributor

Re: Print text per page number

Here are some examples of printing particular sections of files using sed (I've attached the who sed helper, as well).

# print paragraph if it contains AAA (blank lines separate paragraphs)
# HHsed v1.5 must insert a 'G;' after 'x;' in the next 3 scripts below
sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;'

# print paragraph if it contains AAA and BBB and CCC (in any order)
sed -e '/./{H;$!d;}' -e 'x;/AAA/!d;/BBB/!d;/CCC/!d'

# print paragraph if it contains AAA or BBB or CCC
sed -e '/./{H;$!d;}' -e 'x;/AAA/b' -e '/BBB/b' -e '/CCC/b' -e d

# print only lines of 65 characters or longer
sed -n '/^.\{65\}/p'

# print only lines of less than 65 characters
sed -n '/^.\{65\}/!p' # method 1, corresponds to above
sed '/^.\{65\}/d' # method 2, simpler syntax

# print section of file from regular expression to end of file
sed -n '/regexp/,$p'

# print section of file based on line numbers (lines 8-12, inclusive)
sed -n '8,12p' # method 1
sed '8,12!d' # method 2

# print line number 52
sed -n '52p' # method 1
sed '52!d' # method 2
sed '52q;d' # method 3, efficient on large files

# print section of file between two regular expressions (inclusive)
sed -n '/Iowa/,/Montana/p' # case sensitive


(The original document came to me through the courtesy of Princess Paula of Bromley)


Pete

Pete
Carlos Fernandez Riera
Honored Contributor

Re: Print text per page number

See man awk.

It should be :
awk '/Page 2/,/Page 4/ {print $0}' file

but if it is a oracle report then all line should be of same length and all page too, so

i.e.

linesize 80
pagesize 60

to print page 5 to 8

dd if=file of=output bs=4800 skip=5 count=4

will be a faster way.

unsupported
Chris Vail
Honored Contributor

Re: Print text per page number

If you know the last page that printed properly, you need only to know what line number in the file that page is. For example, if the last page that printed properly was had a title of "Page 465", you can find the line number of that by "pr -n -t $FILE|grep "Page 465" but be prepared to wait a bit. Then do a quick line count of the whole file: "wc -l $FILE". Suppose then that the first number is 25575 and the second is 125,425; just subtrac the two, leaving a result of 99,850. Then the command "tail -99850 $FILE |lp " prints the file starting at the last good page.

To print only a few pages in the middle of the file, again you need the line count of the beginning and end of the area you want to print. Call these BEGIN and END line numbers. Get both numbers with the "pr -n -t" command, as above. Then determine the LENGTH in lines that you want, by subtracting the BEGIN from the END. Finally, the command "tail -$BEGIN $FILE|head -$LENGTH|lp " prints just the lines that you identified with the BEGIN and LENGTH numbers.

Alternatively, if you have the line numbers, you can use sed. sed -n '$BEGIN,$ENDp'|lp"

There are probably more than a dozen ways to do this, but these are simple.


Chris
Jason VanDerMark
Trusted Contributor

Re: Print text per page number

Joseph,

Here is my proposed solution:

#! /usr/bin/ksh
# filename: lspages
# usage: lspages
# represents the document to parse
# represents the starting page
# represents the ending page

WIDTH=65
HEIGHT=66
LINES=`echo "$HEIGHT*(($3-$2)+1)" | bc`

pr +$2 -w$WIDTH -l$HEIGHT $1 | head -n$LINES

Cut and paste this script into a file and name it lspages. Then chmod u+x lspages. You can easily adjust the character width and height for each page with the variables in this script.

Here is an example on how to use this script:

lspages /tmp/myfile.txt 30 79 >> pgs30-79.txt

This will print pages 30 through 79 of myfile.txt. I put in no error checking due to the short amount of time I worked on it. Not overly complicated and should work just fine for you.

Regards,
Jason V.
Tie two birds together, eventhough they have four wings, they cannot fly.
Jason VanDerMark
Trusted Contributor

Re: Print text per page number

Joseph,

I know that you will see this, but I felt that I had to correct myself on my example. The example would take those pages and pipe them into the pgs30-79.txt. Again I hope this helps.

Regards,
Jason V.
Tie two birds together, eventhough they have four wings, they cannot fly.