For Programmers: Free Programming Magazines  


Home > Archive > AWK > February 2008 > Precision lost when running gawk program from bat file









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 Precision lost when running gawk program from bat file
di98mase

2008-02-11, 3:58 am

Hi,

when I run a gawk program from a console window I get a different
precision in my results from when I run the same program from a bat
file, why is that?

This is how I run my program in the console window:
H:\semadu2\llt_tests\bl27\1.1.33.1\scripts>gawk -f..\..\..\..\scripts
\llt\llt_ca
lulate_transsaction_times.awk < ..\logs\test2.alc

This gives the following output:
Tr Id USN Diff
----------------------------------------
00027037 0817F7DD 262.614

If I run the same program from a batch file (extract below)
echo "******* Calculate all transaction times ***************"
...\..\..\..\tools\gawk -f ..\..\..\..\scripts\llt
\llt_calculate_transaction_times.awk <..\logs\%1 > ..\results
\transaction_times.res

This gives the following output:
Tr Id USN Diff
----------------------------------------
00027037 0817F7DD 262

As you can se the I loose the decimal precision? Why Is that?
(note that the outputs above are extracts)

Ed Morton

2008-02-11, 6:58 pm



On 2/11/2008 3:40 AM, di98mase wrote:
> Hi,
>
> when I run a gawk program from a console window I get a different
> precision in my results from when I run the same program from a bat
> file, why is that?
>
> This is how I run my program in the console window:
> H:\semadu2\llt_tests\bl27\1.1.33.1\scripts>gawk -f..\..\..\..\scripts
> \llt\llt_ca
> lulate_transsaction_times.awk < ..\logs\test2.alc
>
> This gives the following output:
> Tr Id USN Diff
> ----------------------------------------
> 00027037 0817F7DD 262.614
>
> If I run the same program from a batch file (extract below)
> echo "******* Calculate all transaction times ***************"
> ..\..\..\..\tools\gawk -f ..\..\..\..\scripts\llt
> \llt_calculate_transaction_times.awk <..\logs\%1 > ..\results
> \transaction_times.res
>
> This gives the following output:
> Tr Id USN Diff
> ----------------------------------------
> 00027037 0817F7DD 262
>
> As you can se the I loose the decimal precision? Why Is that?
> (note that the outputs above are extracts)
>


It's almost certainly related to your locale setting. Personally, I've never had
much luck changing locales. The GNU awk manual
(http://www.gnu.org/manual/gawk/html...Conversion.html) says this should work:

$ echo 4,321 | gawk '{ print $1 + 1 }'
-| 5
$ echo 4,321 | LC_ALL=en_DK gawk '{ print $1 + 1 }'
-| 5,321

but here's what I get (gawk 3.1.6 on cygwin):

$ echo 4,321 | gawk '{ print $1 + 1 }'
5
$ echo 4,321 | LC_ALL=en_DK gawk '{ print $1 + 1 }'
5

I've never pursued it as I never need it...

Regards,

Ed.

Toby Darling

2008-02-11, 6:58 pm

> This is how I run my program in the console window:
> H:\semadu2\llt_tests\bl27\1.1.33.1\scripts>gawk -f..\..\..\..\scripts

[...]
> If I run the same program from a batch file (extract below)
> echo "******* Calculate all transaction times ***************"
> ..\..\..\..\tools\gawk -f ..\..\..\..\scripts\llt


While Ed's probably got it with the locale, it looks like you're
running 2 different gawks
di98mase

2008-02-11, 6:58 pm

On 11 Feb, 16:00, Toby Darling <anothercof...@googlemail.com> wrote:
> [...]
>
> While Ed's probably got it with the locale, it looks like you're
> running 2 different gawks


Hi Toby,

I understand why you think so but I am not. The thing is that a have
set the PATH to find gawk. That is why I can use gawk from the scripts
folder in the first example. In the latter example I use a relative
path to the same folder as specified in the PATH.

I also tried the example found in the manual with the same result. I
did notice one thing though. I cant find the LC_ALL variable in a
console window it is only "valid" in cygwin? Also, one thing that
makes no sence is that use a console window in both cases above. I
dont use Cygwin or anything else. I would not suspect windows to
behave different in a console or a batch file...well no precision is
fine for me but it bothers me a bit when I dont know why it happens.

Thanks for your input...
Galen

2008-02-11, 6:58 pm

On Feb 11, 11:08 am, di98mase <di98m...@hotmail.com> wrote:
> On 11 Feb, 16:00, Toby Darling <anothercof...@googlemail.com> wrote:
>
>
>
> Hi Toby,
>
> I understand why you think so but I am not. The thing is that a have
> set the PATH to find gawk. That is why I can use gawk from the scripts
> folder in the first example. In the latter example I use a relative
> path to the same folder as specified in the PATH.
>
> I also tried the example found in the manual with the same result. I
> did notice one thing though. I cant find the LC_ALL variable in a
> console window it is only "valid" in cygwin? Also, one thing that
> makes no sence is that use a console window in both cases above. I
> dont use Cygwin or anything else. I would not suspect windows to
> behave different in a console or a batch file...well no precision is
> fine for me but it bothers me a bit when I dont know why it happens.
>
> Thanks for your input...


Just for grins you might want to try "gawk --version" both
interactively and from a .BAT file, and compare the results. That is,
if you haven't already done so. :-)
Toby Darling

2008-02-11, 6:58 pm

Hi

> I understand why you think so but I am not. The thing is that a have


No problem, just checking.

> I also tried the example found in the manual with the same result. I
> did notice one thing though. I cant find the LC_ALL variable in a
> console window it is only "valid" in cygwin? Also, one thing that
> makes no sence is that use a console window in both cases above. I
> dont use Cygwin or anything else. I would not suspect windows to
> behave different in a console or a batch file...well no precision is
> fine for me but it bothers me a bit when I dont know why it happens.


You'll need to 'set LC_ALL=en_DK' in the window or batch file, but
that doesn't seem to make any difference for me.

I'm no wiser as to what's going wrong either, I can only get the
correct [unrounded] output if I replace 4,321 with 4.321 (in Ed's
example).
Ed Morton

2008-02-11, 6:58 pm



On 2/11/2008 8:26 AM, Ed Morton wrote:
>
> On 2/11/2008 3:40 AM, di98mase wrote:
>
>
>
> It's almost certainly related to your locale setting. Personally, I've never had
> much luck changing locales. The GNU awk manual
> (http://www.gnu.org/manual/gawk/html...Conversion.html) says this should work:
>
> $ echo 4,321 | gawk '{ print $1 + 1 }'
> -| 5
> $ echo 4,321 | LC_ALL=en_DK gawk '{ print $1 + 1 }'
> -| 5,321
>
> but here's what I get (gawk 3.1.6 on cygwin):
>
> $ echo 4,321 | gawk '{ print $1 + 1 }'
> 5
> $ echo 4,321 | LC_ALL=en_DK gawk '{ print $1 + 1 }'
> 5
>
> I've never pursued it as I never need it...
>
> Regards,
>
> Ed.
>


Well, I figured out my problem - cygwin gets its locale implementation from
newlib which only supports C locale (http://sourceware.org/newlib/libc.html#SEC183).

For the OP, your problem may be similair, in which case converting the ","s to
"."s (possibly on-the-fly) should solve your problem:

$ echo 4,321 | gawk '{gsub(/,/,"."); $0 = $1 + 1; gsub(/\./,",")}1'
5,321

Regards,

Ed.

di98mase

2008-02-12, 7:59 am

On 11 Feb, 19:37, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 2/11/2008 8:26 AM, Ed Morton wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
ever had[color=darkred]
uld work:[color=darkred]
>
>
>
>
>
>
>
> Well, I figured out my problem - cygwin gets its locale implementation fro=

m
> newlib which only supports C locale (http://sourceware.org/newlib/libc.htm=

l#SEC183).
>
> For the OP, your problem may be similair, in which case converting the ","=

s to
> "."s (possibly on-the-fly) should solve your problem:
>
> $ echo 4,321 | gawk '{gsub(/,/,"."); $0 =3D $1 + 1; gsub(/\./,",")}1'
> 5,321
>
> Regards,
>
> =A0 =A0 =A0 =A0 Ed.- D=F6lj citerad text -
>
> - Visa citerad text -


Hi again,

I have searched the reference manual for more info about different
locale but without any luck? How shall I write if I want to set the
LC_ALL to swedish for instance? Also, are there more environment
variables that can be set or changed?

Rgds

/di98
Ed Morton

2008-02-12, 7:59 am



On 2/12/2008 6:43 AM, di98mase wrote:
> On 11 Feb, 19:37, Ed Morton <mor...@lsupcaemnt.com> wrote:
>=20
[color=darkred]
never had[color=darkred]
hould work:[color=darkred]
from[color=darkred]
html#SEC183).[color=darkred]
","s to[color=darkred]
>=20
>=20
> Hi again,
>=20
> I have searched the reference manual for more info about different
> locale but without any luck?


Which reference manual? Hopefully the one for your OS rather than the one=
for awk.

> How shall I write if I want to set the
> LC_ALL to swedish for instance?


If it's not in your OS documentation, you stand a better chance of gettin=
g the
right answer if you post that question to an OS-specific NG. For example,=
in
this case with GNU awk, as we discovered you'll get a different answer if=
you're
on cygwin or Solaris.

> Also, are there more environment
> variables that can be set or changed?


Yes and they should be in the documentation for the specific awk that you=
use,
but they'll probably be spread out in various sections based on the
functionality they affect. Sometimes it's undocumented, though, such as
"WHINY_USERS" (seriously!) for gawk.

Ed.

di98mase

2008-02-12, 6:58 pm

On 12 Feb, 14:32, Ed Morton <mor...@lsupcaemnt.com> wrote:
> On 2/12/2008 6:43 AM, di98mase wrote:
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
never had[color=darkred]
ould work:[color=darkred]
>
>
>
>
>
>
>
rom[color=darkred]
tml#SEC183).[color=darkred]
>
,"s to[color=darkred]
>
>
>
>
>
>
>
> Which reference manual? Hopefully the one for your OS rather than the one =

for awk.
>
>
> If it's not in your OS documentation, you stand a better chance of getting=

the
> right answer if you post that question to an OS-specific NG. For example, =

in
> this case with GNU awk, as we discovered you'll get a different answer if =

you're
> on cygwin or Solaris.
>
>
> Yes and they should be in the documentation for the specific awk that you =

use,
> but they'll probably be spread out in various sections based on the
> functionality they affect. Sometimes it's undocumented, though, such as
> "WHINY_USERS" (seriously!) for gawk.
>
> =A0 =A0 =A0 =A0 Ed.- D=F6lj citerad text -
>
> - Visa citerad text -


Ok, thanks all for your help. It is much appreciated!
Sponsored Links







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

Copyright 2008 codecomments.com