For Programmers: Free Programming Magazines  


Home > Archive > AWK > April 2005 > Extracting number from line









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Extracting number from line
R.K.

2005-04-11, 3:56 pm

Hi all,

I'm racking my brain the whole day long for a rather simple question.
Please help me with your infinite knowledge.

I want to count the number of pages in a postscript file by using the
psselect-command according to

psselect -p- file.ps >/dev/null

Example: me@home: psselect -p- input.ps >/dev/null
me@home: [1] [2] [3] [4] [5] [6] Wrote 6 pages, 603850 bytes

And now, I want to extract the page number (here: 6) to process it for other
things. How do I extract this number from this line? Because I want to use the
commands within a shell script and apply it generally the output line shows
the page number at different positions within the line (thus 'cut' doesnt seem
to work here properly).
Any suggestions ?

thanks,
rncdnet
Kenny McCormack

2005-04-11, 3:56 pm

In article <ba35ffd8.0504110629.67f3027b@posting.google.com>,
R.K. <rncdnet@netscape.net> wrote:
>Hi all,
>
>I'm racking my brain the whole day long for a rather simple question.
>Please help me with your infinite knowledge.
>
>I want to count the number of pages in a postscript file by using the
>psselect-command according to
>
>psselect -p- file.ps >/dev/null
>
>Example: me@home: psselect -p- input.ps >/dev/null
> me@home: [1] [2] [3] [4] [5] [6] Wrote 6 pages, 603850 bytes
>
>And now, I want to extract the page number (here: 6) to process it for other
>things. How do I extract this number from this line? Because I want to use the
>commands within a shell script and apply it generally the output line shows
>the page number at different positions within the line (thus 'cut' doesnt seem
>to work here properly).
>Any suggestions ?


Don't use cut. cut is stupid. And this is an AWK newsgroup.

I suppose the following shell would work:

x=$(psselect -p- input.ps 2>&1 >/dev/null | gawk 'sub(/^.*Wrote */,"") {print $1}')

Ed Morton

2005-04-11, 3:56 pm



Kenny McCormack wrote:
> In article <ba35ffd8.0504110629.67f3027b@posting.google.com>,
> R.K. <rncdnet@netscape.net> wrote:
>
>
>
> Don't use cut. cut is stupid. And this is an AWK newsgroup.
>
> I suppose the following shell would work:
>
> x=$(psselect -p- input.ps 2>&1 >/dev/null | gawk 'sub(/^.*Wrote */,"") {print $1}')
>

No need for sub:

x=$(psselect -p- input.ps 2>&1 >/dev/null |gawk '/Wrote/{print $(NF-3)}'

Ed.
Loki Harfagr

2005-04-11, 3:56 pm

Le Mon, 11 Apr 2005 07:29:06 -0700, R.K. a écrit_:

> Hi all,
>
> I'm racking my brain the whole day long for a rather simple question.
> Please help me with your infinite knowledge.
>
> I want to count the number of pages in a postscript file by using the
> psselect-command according to
>
> psselect -p- file.ps >/dev/null
>
> Example: me@home: psselect -p- input.ps >/dev/null
> me@home: [1] [2] [3] [4] [5] [6] Wrote 6 pages, 603850 bytes
>
> And now, I want to extract the page number (here: 6) to process it for other
> things. How do I extract this number from this line? Because I want to use the
> commands within a shell script and apply it generally the output line shows
> the page number at different positions within the line (thus 'cut' doesnt seem
> to work here properly).
> Any suggestions ?


An overkill in fonky style but it ensures to get what's between
"Wrote" and " pages,"
$ awk 'BEGIN{RS=" pages,";FS="Wrote "};/Wrote /,/ pages,/{print $2}'

Kenny McCormack

2005-04-11, 3:56 pm

In article <KpWdnTbqr8nNDcffRVn-tg@comcast.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:
....
>No need for sub:
>
>x=$(psselect -p- input.ps 2>&1 >/dev/null |gawk '/Wrote/{print $(NF-3)}'
>
> Ed.


6 of one, half dozen of the other.

Mine seems a little more generic. YMMV.

Patrick TJ McPhee

2005-04-12, 3:56 am

In article <ba35ffd8.0504110629.67f3027b@posting.google.com>,
R.K. <rncdnet@netscape.net> wrote:

% Please help me with your infinite knowledge.

I assume this means you want a lot of answers.

% Example: me@home: psselect -p- input.ps >/dev/null
% me@home: [1] [2] [3] [4] [5] [6] Wrote 6 pages, 603850 bytes

You could use
(psselect -p- input.ps >/dev/null) 2>&1 |
awk -F'Wrote | page' '/Wrote/ { print $2 }'

On the other hand, since psselect depends on the postscript file
following the document structuring conventions, you don't really
need to use it, just parse out the relevant DSC yourself:

awk '/^%%Pages: [0-9]+/ { print $NF; exit }' input.ps

or count all the '/^%%Page:/' entries.
--

Patrick TJ McPhee
North York Canada
ptjm@interlog.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com