For Programmers: Free Programming Magazines  


Home > Archive > AWK > February 2007 > Re: Newbie AWK problem.









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 Re: Newbie AWK problem.
Chris UK

2007-02-14, 7:57 am

On 14 Feb, 13:23, gaze...@xmission.xmission.com (Kenny McCormack)
wrote:
> In article <1171459179.590863.300...@l53g2000cwa.googlegroups.com>,
>
>
>
>
>
> Chris UK <chris.bai...@myrealbox.com> wrote:
>
>
>
>
>
>
>
>
>
> I thought the goal was specifically to get rid of any trailing ".0".
>
> Maybe time to get my crystal ball serviced.- Hide quoted text -
>
> - Show quoted text -


Kenny,

Maybe a little more explanation is in order. Prior to this point in
the script I have parsed a command output and collected key result in
two arrays that are indexed by the same number. The arrays are
results which contains a single space delimited word, and units which
containts a number of space delimited words.

I arrange that the result variable is always a numeric value and then
pass it back into awk for floating point numeric comparison to a
predefined set of limits (in another pair of arrays, same numerical
index).

The problem comes with the 'word' contains more than a numerical
value, and this is what I am attempting to return to (problems occur
with values such as "10%", "$10", "8/" <- this last one comes from a
string such as "Surveys show that 8/ 10 cats prefer not being eaten by
dogs").

The intention is to remove all non numeric (/[^0-9]/) or non decimal
point characters ([^\.]).

Does this help clarify what I am attempting to achieve?

Many thanks for your kind assistance.

All best,
Chris.

Kenny McCormack

2007-02-14, 7:57 am

In article <1171459974.875933.52520@p10g2000cwp.googlegroups.com>,
Chris UK <chris.bailes@myrealbox.com> wrote:
....
....[color=darkred]
>The problem comes with the 'word' contains more than a numerical
>value, and this is what I am attempting to return to (problems occur
>with values such as "10%", "$10", "8/" <- this last one comes from a
>string such as "Surveys show that 8/ 10 cats prefer not being eaten by
>dogs").
>
>The intention is to remove all non numeric (/[^0-9]/) or non decimal
>point characters ([^\.]).


Possibilities:
1) If you are willing to let AWK do the string-to-number
conversion for you, which is generally a Good Thing, but still
want to preserve trailing ".0", which is generally a Bad
Thing (I'm still not sure what you think you need to do
this, since in your explanation, you state that you are just
comparing them to other numbers), you might try:

{ printf("%.2f\n",$1) }

2) If you really do to support leading and/or embedded garbage
(e.g., "$10" or "foo12bar9.junk5") then you will indeed need
to use gsub. Something like:

{ gsub(/[^0-9.]*/,"");print }

Sponsored Links







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

Copyright 2008 codecomments.com