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:
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.
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.
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:
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:
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
Any thoughts? I'm thoroughly confused.
Last edited by treesloth; 06-24-2010 at 04:17 PM..
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)
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)
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)
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)
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)
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)
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)
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)