Strange variable comparison result in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Strange variable comparison result in awk
# 1  
Old 06-23-2010
Strange variable comparison result in awk

So, I'm making a little awk script that generates a range-based histogram of a set of numbers. I've stumbled onto a strange thing. Toward the end of the process, I have this test:

if ( bindex < s )

"bindex" is the "index" of my "bin" (the array element that gets incremented whenever a number in the appropriate range is found. "s" is the "start" value-- that is, everything below that value is lumped together in a single bin. Now, here's the weird thing. I knew that I should have particular output. My script's output was exactly 2 higher, so I added a troubleshooting line:

print bindex "\t" bin[bindex] "\t" s

That should tell me everything that the script thinks is less than my start value. Here's the output:

1750 30 2000
1875 227 2000
10250 1 2000
12625 1 2000

The correct answer really is 257, but the above would produce 259, and as you can see, my awk script believes that 12625 is less than 2000, as is 10250! What gives? Shouldn't this be purely numeric, and so purely numeric rules apply? If not, how do I get it to use only numeric rules, such that 12625 is greater than 2000 like all those math teachers were so convinced of? Many thanks in advance.
# 2  
Old 06-23-2010
Not really sure if I got you right. If the correct answer is 257, then that's probably the sum of all the second columns, if the first column is less than third column.
Code:
awk '{if($1<$3) s+=$2}END{print s}' file

# 3  
Old 06-24-2010
Perhaps some context will help. This is incomplete, of course. I haven't yet added outputs for all results-- I'm just working on the "lowest" bin and rudimentary output for the rest. Once that works, I'll finish it, which should be easy enough.

So, what I do is take a number and increment a particular "bin"-- an array element-- depending on the value of that number. That's handled by this line:

Code:
bin[ ( i * int(($0 - 1) / i) + i)]++

Now, sometimes it's useful to be able to lump together everything below a certain value. Since the index of the array is simply the number describing the histogram values contained in the bin, I can just tell the script, "take all array elements whose index is less than the user-specified start value and dump them all into a single bin". I do that by the simple numeric test I described above. The problem, though, is that those values are not being properly tested. Why in the world would this script believe that 10000 is less than 2000? As special troubleshooting output, I have the script tell me which "bins" it's putting in the "everything less than user threshold" group. It puts these lines at the top, followed by the "normal" output. Here's a sample of numbers such as those that it will be used to test, sorted for reader convenience:

Code:
139
384
515
552
556
2128
2183
2234
2277
2284
2331
2385
2879
3281
3327
5035
5294
9401
9948
11458
13363

As you can see, there are 5 values below 2000-- yet, for some reason, the test says that there are 3, and the 3 it gives are over 10000. It almost seems to be applying some sort of alphabetic test, not numeric.

To test, save the values above as, say, "testvals". Save the script below as "hist.awk" and run:

./hist.awk -v i="125" -v s="2000" testvals

Code:
#! /usr/bin/awk -f

        {
        bin[ ( i * int(($0 - 1) / i) + i)]++
        }

END     {
        for (bindex in bin)
                {
                if ( bindex < s )
                        {
                        print bindex "\t" bin[bindex] "\t" s
                        hist[less] = hist[less] + bin[bindex]
                        }
                else
                        {
                        hist[bindex] = bin[bindex]
                        }
                }
                print "less\t" hist[less]
        }

Any thoughts? I'm thoroughly confused.

Last edited by treesloth; 06-24-2010 at 04:17 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk if comparison with variable

Hi , Need help, p for value in `awk -F, '{print $1 }' ad | uniq` do x=$(echo $value) echo $x echo `awk -F, '{if( $1 == $x) sum = sum + $8 } END{print sum}' ad` --- not working echo `awk -F, '{if($1 == “MT”) sum = sum + $8 } END{print sum}' ad` -- Working but hard coded done; ad... (4 Replies)
Discussion started by: nadeemrafikhan
4 Replies

2. UNIX for Dummies Questions & Answers

Strange result using find command.

I created a file with the permissions of 776. When I ran the command find /root/Desktop -perm -644 -type f The created file shows up as part of the results. Doesn't -perm -mode mean that for global, only 4(read) and 2(write) can be accepted ? (2 Replies)
Discussion started by: Hijanoqu
2 Replies

3. Shell Programming and Scripting

Assign awk gsub result to a variable

Hello, I have searched but failed to find what exactly im looking for, I need to eliminate first "." in a output so i can use something like the following echo "./abc/20141127" | nawk '{gsub("^.","");print}' what i want is to use gsub result later on, how could i achieve it? Let say... (4 Replies)
Discussion started by: EAGL€
4 Replies

4. Shell Programming and Scripting

Strange result

Hi, I have following codes which looks ok: $ string1="123456789 abc2" $ string2="abc" $ position_of_string2=`expr index "$string1" "$string2"` $ echo $position_of_string2 $ 11however, when string2="abc2", it gives me the following result: $ string1="123456789 abc2" $... (5 Replies)
Discussion started by: littlewenwen
5 Replies

5. Shell Programming and Scripting

Strange result of eval, how does eval really work with ssh?

Hi all, some small script with eval turned me to crazy. my OS is linux Linux s10-1310 2.6.16.53-0.8.PTF.434477.3.TDC.0-smp #1 SMP Fri Aug 31 06:07:27 PDT 2007 x86_64 x86_64 x86_64 GNU/Linux below script works well #!/bin/bash eval ssh remotehost date eval ssh remotehost ls below... (1 Reply)
Discussion started by: summer_cherry
1 Replies

6. Shell Programming and Scripting

Awk multiple variable array: comparison

Foo.txt 20 40 57 50 22 51 66 26 17 15 63 18 80 46 78 99 87 2 14 14 51 47 49 100 58 Bar.txt 20 22 51 15 63 78 99 55 51 58 How to get output using awk 20 22 57 50 51 15 26 17 63 78 80 46 99 55 - - 51 58 49 100 (5 Replies)
Discussion started by: genehunter
5 Replies

7. Shell Programming and Scripting

awk comparison with variable

hi, I want to compare i variable in the awk statement but its not working out. Pl help me out If we do the comparison like this its OK, cat sample | awk -F" ", '{if ($1=="1-Sep-2009") print $1,$2,$3,$4,$5}' But if u use a variable instead of "1-Sept-2009", it does not return anything,... (2 Replies)
Discussion started by: asadlone
2 Replies

8. Shell Programming and Scripting

awk printing: strange result

Dear all, I am using awk in a bash script to extract a list of x y z coordinates from a file such as: %BEGIN 3D-SPACE COORDINATES 0.2085627338147950 0.2471306816410478 0.2085627338147950 0.1242549179185660 0.2755539793525220 0.4147884486606120 0.2030669560265720 ... (6 Replies)
Discussion started by: pauli
6 Replies

9. Shell Programming and Scripting

assign awk command result to a variable

#!/bin/sh # ## MYSTRING = `awk '/myApp.app/' /Users/$USER/Library/Preferences/loginwindow.plist` if then echo String not found defaults write /Users/$USER/Library/Preferences/loginwindow AutoLaunchedApplicationDictionary -dict-add -string Hide -bool YES -string Path -string... (9 Replies)
Discussion started by: dedmakar
9 Replies

10. UNIX for Advanced & Expert Users

Strange Number comparison issue

Hi, I am comparing two numbers, but it gives strange results: My Code: if then echo "True" else echo "False" fi This code gives False for the follwoing comparison where as True for the following: Any reason for this? Both Should have given False... I am using... (9 Replies)
Discussion started by: shihabvk
9 Replies
Login or Register to Ask a Question