Match real numbers in AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match real numbers in AWK
# 1  
Old 04-29-2010
Match real numbers in AWK

I am looking for a better way to match real numbers within a specified tolerance range. My current code is as follows:

Code:
      if ($1 !~ /^CASE/) for(i=1;i in G;i++) if (G[i] >= $5-1  && G[i] <= $5+1)
         { print $1,$4,$5,J[i],G[i] }
           else { print $1,"NO MATCH" }

where $5 and G[i] are real numbers e.g. "12.456" and "11.543". Right now the code prints out several numbers within the range of 1+ 12.456 and 1-12.456.

Is there a better way of accomplishing this? I would like to get the closest number without manually having to adjust the tolerance range of 1.

Thanks
# 2  
Old 04-30-2010
# 3  
Old 04-30-2010
What drl is telling you:
1. floating point numbers represent values correctly only to a certain level of precision
so the = part of a comparison does not work like you would think.

2. awk uses double precision floating point number for aritmetic operations, so #1 applies.

3. in /usr/include/math.h you will find macros defined:
Code:
int isgreater(real-floating x, real-floating y);
int isgreaterequal(real-floating x, real-floating y);
int isless(real-floating x, real-floating y);
int islessequal(real-floating x, real-floating y);
int islessgreater(real-floating x, real-floating y);

these are the standard way of doing floating point comparisons on your system. Notice there is no "equal()", for reasons just explained.

Here is a good explanation of FP numbers -- the nearly universal IEEE-754 format:
IEEE Standard 754 Floating-Point
# 4  
Old 04-30-2010
How would I apply this to my code? I can't seem to view any of the links right now.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

2. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. Shell Programming and Scripting

Match on a range of numbers

Hi, I'm trying to match a filename that could be called anything from vout001 to vout252 and was trying to do a small test but I'm not getting the result I thought I would.. Can some one tell me what I'm doing wrong? *****@********>echo $mynumber ... (4 Replies)
Discussion started by: Jazmania
4 Replies

4. Shell Programming and Scripting

awk : match only the pattern string , not letters or numbers after that.

Hi Experts, I am finding difficulty to get exact match: file OPERATING_SYSTEM=HP-UX LOOPBACK_ADDRESS=127.0.0.1 INTERFACE_NAME="lan3" IP_ADDRESS="10.53.52.241" SUBNET_MASK="255.255.255.192" BROADCAST_ADDRESS="" INTERFACE_STATE="" DHCP_ENABLE=0 INTERFACE_NAME="lan3:1"... (6 Replies)
Discussion started by: rveri
6 Replies

5. Shell Programming and Scripting

Complex match of numbers between 2 files awk script

Hello to all, I hope some awk guru could help me. I have 2 input files: File1: Is the complete database File2: Contains some numbers which I want to compare File1: "NUMBERKEY","SERVICENAME","PARAMETERNAME","PARAMETERVALUE","ALTERNATENUMBERKEY"... (9 Replies)
Discussion started by: Ophiuchus
9 Replies

6. Shell Programming and Scripting

Arithmetic calculation on real numbers in Bourne Shell Script

I am begining to learn bourne shell and as a practice I have written a script which when given the purchase price and percentage of discount calculates the savings. I somehow cannot figure out why my script fails to do arthimatic calculation on real numbers. Could anyone look at the script... (5 Replies)
Discussion started by: Tirmazi
5 Replies

7. Shell Programming and Scripting

match range of different numbers by AWK

if the column1 and 2 in both files has same key (for example "a" and "a1") compare each first key value(a1 of a) of input2 (for example 1-4 or 65-69 not 70-100 or 44-40 etc) with all the values in input1. if the range of first key value in input2 is outof range in input1 values named it as out... (54 Replies)
Discussion started by: repinementer
54 Replies

8. Shell Programming and Scripting

how to compare big real numbers

Hi everyone, I need to compare 2 big Floating/Real numbers in a script. After the comparission it is showing worng values in my script. echo "Enter value1" read value1 echo "Enter value2" read value2 Result=`echo "if($value1 > $value2) 1" | bc` if ; then echo "$value1 is... (4 Replies)
Discussion started by: padarthy
4 Replies

9. Shell Programming and Scripting

How to Compare Floating point / real numbers

Hai, Can you please guide me, to compare the floating point numbers. Eg. If then echo "value1 is grater " fi This code is not working properly when i excuted with floating values or real numbers (13 Replies)
Discussion started by: padarthy
13 Replies

10. Shell Programming and Scripting

match numbers (awk)

i would like to enter (user input) a bunch of numbers seperated by space: 10 15 20 25 and use awk to print out any lines in a file that have matching numbers so output is: 22 44 66 55 (10) 77 (20) (numbers 10 and 20 matched for example) is this possible in awk . im using gawk for... (5 Replies)
Discussion started by: tanku
5 Replies
Login or Register to Ask a Question