LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 06-07-2010, 01:15 AM   #1
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
Awk issue !!


The following code snippet it that is hanging me up.

The value of c and b are equal numerically. But with the operation I am doing with c the value of c is implicity changing to type string and the comparision is then for string not for numeric values. (My understanding)

Code:
BEGIN   {
        a = 0
        b = 0
        c = 0

c=sprintf("%5.2f",b)
gsub(" ","0",c)
gsub("[.]","",c)

print "c = "c
print "b = "b

if ( c != b )
{
        print "b and c are not equal"
}
else
{
        print "c and b are equal"
}

}
And if I force c to be numeric before comparison

by

Code:
c+=0
The comparison becomes numeric but the value of c is trimmed to a single 0.

Is there any way to cope with this. i.e comparison to be numeric without typecasting.

I am using GNU Awk 3.1.5
 
Old 06-07-2010, 01:44 AM   #2
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Hi PMP

Your issue would seem to stem from the fact you are using a string based function (namely sprintf) to alter your numeric value.
Quote:
sprintf(format, expression1, ...)
This returns (without printing) the string that printf would have printed out with the same arguments (see Printf).
Once you pass this line you now have a string.
 
Old 06-07-2010, 02:05 AM   #3
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Original Poster
Rep: Reputation: 58
Correct !!

Not sure why but I have an HP-UX 11.00 box and this code work fine there !!
 
Old 06-07-2010, 04:12 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Same version of [g]awk?
 
Old 06-07-2010, 04:14 AM   #5
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Original Poster
Rep: Reputation: 58
No, unable to figure out what version is there. Hopefully it is the one that comes with HP UX box
 
Old 06-07-2010, 04:20 AM   #6
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Original Poster
Rep: Reputation: 58
Got this from gawk man page
Quote:
Gawk performs comparisons as follows: If two variables are
numeric, they are compared numerically. If one value is
numeric and the other has a string value that is a
``numeric string,'' then comparisons are also done numeri-
cally. Otherwise, the numeric value is converted to a
string and a string comparison is performed. Two strings
are compared, of course, as strings.
According to the
POSIX standard, even if two strings are numeric strings, a
numeric comparison is performed. However, this is clearly
incorrect, and gawk does not do this.

Not able to understand the part marked in Red
 
Old 06-07-2010, 09:17 PM   #7
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Quote:
No, unable to figure out what version is there. Hopefully it is the one that comes with HP UX box
hmmm... so this does not work:
Code:
gawk --version
And I think I can make sense of the test not working.

When you do the print prior to the test of 'c' it is four (4) zeroes (0): c = 0000
You have converted this to a string so you can see the padding and therefore it is now a string.

The bit that you think should work is:
Quote:
If one value is numeric and the other has a string value that is a ``numeric string,''
The problem with this is that the string "0000" is not numeric where as "0" is.

As a proof to this I changed the code:
Code:
BEGIN   {
    a = 0
    b = 0
    c = 0

    c=sprintf("%5.2f",b)
    gsub(" ","",c)
    gsub("[.]00","",c) #now c is just equal to a single 0

    print "c = "c
    print "b = "b

    if ( c != b )
        print "b and c are not equal"
    else
        print "c and b are equal"
}
This now produces the results you seek.
 
Old 06-08-2010, 02:14 AM   #8
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Original Poster
Rep: Reputation: 58
No, HP UX box has awk that comes with HP UX, not gwak, so this code will not return anything.

Code:
gawk --version
Quote:
If one value is
numeric and the other has a string value that is a
``numeric string,'' then comparisons are also done numeri-
cally.
Here c becomes a string as I am assigning the return of sprintf to it, b is still numerical.

Now with the avobe statement the comparision should be numerical. But it's not. Confused !!

Aditionally I tried --compat, --traditional modes with gwak to behave it like awk, but that's not as well.

Last edited by PMP; 06-08-2010 at 02:15 AM.
 
Old 06-08-2010, 03:20 AM   #9
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,698

Rep: Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988Reputation: 1988
Quote:
Now with the avobe statement the comparision should be numerical. But it's not. Confused !!
No the comparison will not be numeric as you have not passed it a 'numeric string'.

Numeric string is - "1" or "1234"

But is not - "01" or "0001234"

As soon as you pad the number it is no longer recognised as a numeric value but a string containing those characters.

You could throw int() at the value of c and convert it back but the i would not see the point of creating c the way you have??
 
  


Reply

Tags
awk, string


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] awk: how can I assign value to a shell variable inside awk? quanba Programming 6 03-23-2010 03:18 AM
awk variable issue cdestiny Linux - General 5 03-11-2010 12:26 AM
awk , I need help for awk, just a display function mcandy General 1 12-15-2008 01:21 PM
shell command using awk fields inside awk one71 Programming 6 06-26-2008 05:11 PM
AWK/SED Multiple pattern matching over multiple lines issue GigerMalmensteen Programming 15 12-03-2006 06:08 PM


All times are GMT -5. The time now is 05:28 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration