I need to detect the number of pages in a print job when it is available so I can warn users when they try to print a report much larger than they expected. Sometimes they are trying to print 1000 page reports when they thought they were getting a 10 page report.
Under linux I am scanning the print job before it goes to the printer and want the user to be able to cancel the print job if it is not what they expected.
This current function works, but is not exactly what I wanted:
function get-last-page()
{
awk '/Page/{ f=$NF }; END{ print f }' $1 | awk '{ sub(/Page/,""); print }';
}
In my test case the page numbers can be found on the top of each page after the word "Page". -but- sometimes the page count abuts the "Page" label so the above function could return "Page2308" when there is no white space between the regex "Page" and the desired number. There is no problem when there is white space.
Simple answer, just sub out the /Page/ unconditionally and I always get the last page number of the report. -but- I originally tried it like this (using only one awk call) and I cannot for the life of me make it work properly:
function get-last-page()
{
awk '/Page/{ f=$NF }; END{ print sub(/Page/,"",f); }' $1;
}
The above function does not return the desired 2308 (or whatever). I cannot make out what is wrong. If I change it to the following I get what I expect, including the "Page2308":
function get-last-page()
{
awk '/Page/{ f=$NF }; END{ print f }' $1;
}
The above behaves as I expect, but has the error I am trying to fix with the second example. I don't understand why I can't do the substitution in the END{} clause but can do it after the pipe (as in the first example).
I really was hoping to get this to work as a sort-of one-liner.
Can anyone help me either fix this or at least understand why it does not work?
I am running under a bash shell on openSuSE 10.3 linux.
TIA