Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

n00b PostScript files & decimal places

27 views
Skip to first unread message

Justin Fox

unread,
May 24, 2013, 3:44:43 PM5/24/13
to
This is probably really easy (I hope). I have been looking at awk for just over a week.

This is a line from my data file:
%SSiPrshPage: 0.00000 0.00000 198.00000 198.00000 0 1 0 0.00000 0.00000 0.00000 0.00000 16388 0.00000 1.00000 0.00000 1 0.00000 0.00000 1

$2 and $3 are the X and Y coordinates of this PS object. I need to add 97.92 to $2 and 131.76 to $3 for this object to be in the correct position. I need the output to have 5 decimal places. For simplicities sake I am testing just $2.

I can do the math:
awk '{ $2 += 97.92 ; print $2 }' 135oneline.txt
which returns 97.92

I just need the output to be formatted like it want it.

I have tried:
awk '{ $2 += 97.92 ; fprint "%5d" $2 }' 135oneline.txt
which returns nothing

awk '{ $2 += 97.92 ; fprint "%5g" $2 }' 135oneline.txt
which returns nothing

awk '{ $2 += 97.92 ; fprint "%.5d" $2 }' 135oneline.txt
which returns nothing

gawk '{ $2 += 97.92 ; printf "%6.5f" $2 }' 135oneline.txt
which returns
gawk: cmd. line:1: (FILENAME=135oneline.txt FNR=1) fatal: not enough arguments to satisfy format string
`%6.5f97.92'
^ ran out for this one

What am I missing?

Kenny McCormack

unread,
May 24, 2013, 3:51:06 PM5/24/13
to
In article <e938cc17-8845-41f3...@googlegroups.com>,
Justin Fox <foxpj...@gmail.com> wrote:
>This is probably really easy (I hope). I have been looking at awk for just
>over a week.
>
>This is a line from my data file:
>%SSiPrshPage: 0.00000 0.00000 198.00000 198.00000 0 1 0 0.00000 0.00000 0.00000
>0.00000 16388 0.00000 1.00000 0.00000 1 0.00000 0.00000 1
>
>$2 and $3 are the X and Y coordinates of this PS object. I need to add 97.92 to
>$2 and 131.76 to $3 for this object to be in the correct position. I need the
>output to have 5 decimal places. For simplicities sake I am testing just $2.
>
>I can do the math:
>awk '{ $2 += 97.92 ; print $2 }' 135oneline.txt
>which returns 97.92
>
>I just need the output to be formatted like it want it.

I think what you are looking for is:

{ $2 += 97.92; $3 += 131.76; print }

There are caveats and stuff associated with this; no doubt, other
readers/posters will tell you about the possible issues.

>gawk '{ $2 += 97.92 ; printf "%6.5f" $2 }' 135oneline.txt
>which returns
>gawk: cmd. line:1: (FILENAME=135oneline.txt FNR=1) fatal: not enough arguments to
>satisfy format string
> `%6.5f97.92'
> ^ ran out for this one
>
>What am I missing?

The syntax of the *printf* functions requires a comma after the format
string (and also between each of the other arguments). So, you are
probably looking for:

printf "%6.5f",$2

As an aside, the reason you got the above error message is that what you
wrote gets parsed as:

printf "a string"

with no args supplied. The value of "a string" is the concatenation of
"%6.5f" and $2.

--
Just for a change of pace, this sig is *not* an obscure reference to
comp.lang.c...

Anton Treuenfels

unread,
May 24, 2013, 6:43:59 PM5/24/13
to

"Justin Fox" <foxpj...@gmail.com> wrote in message
news:e938cc17-8845-41f3...@googlegroups.com...
Is that the only line you're interested in changing? If so, since both
values are zero it might be simpler just to substitute the strings as you
want them directly, rather than going through printf():

{ $2 = "97.92000"; $3 = "131.76000"; print }

A more roundabout way of accomplishing this might be to dynamically create a
new format string, ie., change this:

%SSiPrshPage: 0.00000 0.00000 198.00000 198.00000 0 1 0 0.00000 0.00000
0.00000 0.00000 16388 0.00000 1.00000 0.00000 1 0.00000 0.00000 1

to this:

%SSiPrshPage: %.5f %.5f 198.00000 0 1 0 0.00000 0.00000 0.00000 0.00000
16388 0.00000 1.00000 0.00000 1 0.00000 0.00000 1

and use that to print the new line, perhaps something like this (untested):

{
f2val = $2 + 97.92
f3val = $3 + 131.76
$2 = "%.5f"
$3 = "%.5f"
printf( $0, f2val, f3val )
}

or if using $0 as a format string makes you uncomfortable (I confess I've
never done it myself), maybe assign $0 to a string you feel good about
manipulating and use one of the subs() family functions to change the
contents of fields $2 and $3 instead.

- Anton Treuenfels


0 new messages