Numeric test behaves differently in Linux and Cygwin - awk

This is a discussion on Numeric test behaves differently in Linux and Cygwin - awk ; 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 ...

+ Reply to Thread
Results 1 to 6 of 6

Numeric test behaves differently in Linux and Cygwin

  1. Default Numeric test behaves differently in Linux and Cygwin

    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?


  2. Default Re: Numeric test behaves differently in Linux and Cygwin

    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.



  3. Default Re: Numeric test behaves differently in Linux and Cygwin

    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=

  4. Default Re: Numeric test behaves differently in Linux and Cygwin

    Kenny McCormack wrote:

    > 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.]+$/



    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....

    >
    >>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.


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

    Ed.

  5. Default Re: Numeric test behaves differently in Linux and Cygwin

    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.


  6. Default Re: Numeric test behaves differently in Linux and Cygwin

    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

    ******************************************************

+ Reply to Thread

Similar Threads

  1. std::getline() behaves differently between platforms?
    By Application Development in forum c++
    Replies: 6
    Last Post: 11-22-2007, 06:10 AM
  2. info globals behaves differently in 8.4.6 and 8.4.9
    By Application Development in forum TCL
    Replies: 1
    Last Post: 04-25-2007, 09:10 AM
  3. info globals behaves differently in 8.4.6 and 8.4.9
    By Application Development in forum TCL
    Replies: 0
    Last Post: 04-25-2007, 02:31 AM
  4. Imaging Codec Test behaves differently in different Test environme
    By Application Development in forum Software-Testing
    Replies: 1
    Last Post: 02-26-2007, 03:50 PM
  5. Font behaves differently in CS2 vs InDesign
    By Application Development in forum Adobe illustrator
    Replies: 2
    Last Post: 12-13-2006, 09:49 AM