Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Extracting number from line
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 t
he
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 se
em
to work here properly).
Any suggestions ?

thanks,
rncdnet

Report this thread to moderator Post Follow-up to this message
Old Post
R.K.
04-11-05 08:56 PM


Re: Extracting number from line
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 othe
r
>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 s
eem
>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 */,"") {prin
t $1}')


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
04-11-05 08:56 PM


Re: Extracting number from line

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 */,"") {pr
int $1}')
>
No need for sub:

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

Ed.

Report this thread to moderator Post Follow-up to this message
Old Post
Ed Morton
04-11-05 08:56 PM


Re: Extracting number from line
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 oth
er
> 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 show
s
> 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}'


Report this thread to moderator Post Follow-up to this message
Old Post
Loki Harfagr
04-11-05 08:56 PM


Re: Extracting number from line
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.


Report this thread to moderator Post Follow-up to this message
Old Post
Kenny McCormack
04-11-05 08:56 PM


Re: Extracting number from line
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

Report this thread to moderator Post Follow-up to this message
Old Post
Patrick TJ McPhee
04-12-05 08:56 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

AWK archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:07 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.