For Programmers: Free Programming Magazines  


Home > Archive > AWK > January 2006 > Numeric test behaves differently in Linux and Cygwin









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 Numeric test behaves differently in Linux and Cygwin
Martin

2006-01-30, 7:55 am

I'm using

awk ' (( $1+0 == $1 )) {print $1, $2, $3}' < input

to print only lines in which the first field is numeric. This works
fine on Cygwin, but running the same script on Ubuntu 5.10 requires
that $1 is an integer, so it matches 44 but not 0.44.

The awk version is gnu awk 3.1.4 on both systems.

Any ideas why?

Kenny McCormack

2006-01-30, 7:55 am

In article <1138626711.452539.136570@g47g2000cwa.googlegroups.com>,
Martin <loveslave@frustratedhousewives.zzn.com> wrote:
>I'm using
>
>awk ' (( $1+0 == $1 )) {print $1, $2, $3}' < input


ITYM

$1 ~ /^[0-9.]+$/

>to print only lines in which the first field is numeric. This works
>fine on Cygwin, but running the same script on Ubuntu 5.10 requires
>that $1 is an integer, so it matches 44 but not 0.44.


Juergen Kahrs

2006-01-30, 6:56 pm

Martin wrote:

> awk ' (( $1+0 == $1 )) {print $1, $2, $3}' < input
>
> to print only lines in which the first field is numeric. This works
> fine on Cygwin, but running the same script on Ubuntu 5.10 requires
> that $1 is an integer, so it matches 44 but not 0.44.


This may be a _locale_ problem. Try this on both platforms
and compare the results:

> locale

LANG=POSIX
LC_CTYPE="POSIX"
LC_NUMERIC="POSIX"
LC_TIME="POSIX"
LC_COLLATE=POSIX
LC_MONETARY="POSIX"
LC_MESSAGES="POSIX"
LC_PAPER="POSIX"
LC_NAME="POSIX"
LC_ADDRESS="POSIX"
LC_TELEPHONE="POSIX"
LC_MEASUREMENT="POSIX"
LC_IDENTIFICATION="POSIX"
LC_ALL=
Ed Morton

2006-01-30, 6:56 pm

Kenny McCormack wrote:

> In article <1138626711.452539.136570@g47g2000cwa.googlegroups.com>,
> Martin <loveslave@frustratedhousewives.zzn.com> wrote:
>
>
>
> ITYM
>
> $1 ~ /^[0-9.]+$/



That would allow multiple periods, which may be OK for some locales that
separate their "thousands" by periods, but for others that only allow
one period that'd give false matches. It'd also match on a string that's
just a single period and it won't match negantive numbers, etc....
[color=darkred]
>

Define "numeric". Is 3e04 numeric? How about 1,024? How about -7?

Ed.
Martin

2006-01-30, 6:56 pm

Yes, that makes sense. The Linux installation has a Swedish locale, so
it probably expects numbers to have decimal commas instead of points.

Thanks for the hint.

Terry D. Boldt

2006-01-31, 9:55 pm

Martin wrote:
> I'm using
>
> awk ' (( $1+0 == $1 )) {print $1, $2, $3}' < input
>
> to print only lines in which the first field is numeric. This works
> fine on Cygwin, but running the same script on Ubuntu 5.10 requires
> that $1 is an integer, so it matches 44 but not 0.44.
>
> The awk version is gnu awk 3.1.4 on both systems.
>
> Any ideas why?
>



Try this: awk ' (( $1+0.0 == $1 )) {print $1, $2, $3}' < input

that should convert $1 to a floating point instead of an integer. To get
both floating point and integer try two compariasons

awk ' (( ($1+0.0 == $1) || ($1+0 == $1) )) {print $1, $2, $3}' < input
--
++++++++++++++++++++++++++++++++++++++++
++++++++++++++
========================================
==============
****************************************
**************
If you are always rushing towards the future,
Then you never have any past.

Terry Boldt
****************************************
**************
Paraphrasing Ben Franklin:

Those who sacrifice freedom for safety, have neither.

The exact quote:

They that can give up essential liberty to obtain a little
temporary safety deserve neither liberty nor safety.
Benjamin Franklin (1706 - 1790),
US author, diplomat, inventor, physicist, politician, & printer
Historical Review of Pennsylvania, 1759

****************************************
**************
Sponsored Links







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

Copyright 2008 codecomments.com