Closest Number from a Range of Numbers


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Closest Number from a Range of Numbers
# 1  
Old 08-16-2011
Closest Number from a Range of Numbers

out of a range of numbers, how can i pick out the number that is the closest to any arbitrary/random number that a user supplies?

say the range of numbers are between 1 - 90000. but that doesn't mean each number exist between 1 - 90000. the range of numbers could be for example:

Code:
1, 3, 4, 6, 10, 30, 48, 49, 50, 51......89999, 90000

As you can see, the numbers are in order from smallest to greatest, however, some numbers are missing. Say a user provides a number that is missing in the range of numbers. How do I grab the closest number in that range to what the user provided?

like say the user provided the number 39. From looking at the above range, you can see that the number 35 doesn't exist. So, how do I know to pick the closest number to 39 that does exist in the range? The closest numbers to 39, in the above range, are 30 and 48. How can I do this in a bash shell script?

I hope i'm making sense to you guys.

Last edited by SkySmart; 08-16-2011 at 12:25 PM..
# 2  
Old 08-16-2011
A typical task for binary search. But if you don't care about efficiency this works (GNU grep and bash):
Code:
nums='1, 3, 4, 6, 10, 30, 48, 49, 50, 51, 89999, 90000'

num=39

echo $nums, $num | tr , '\n' | sort -n | grep -C1 $num | xargs |             
  read a b c d; if ((b-a <= c-b));then echo $a; else echo $c; fi


Last edited by yazu; 08-16-2011 at 12:52 PM..
# 3  
Old 08-16-2011
Code:
 awk -v VAL=5 -v RS="," '{
        D=(VAL - $1) * (VAL - $1);
        if((!SET) || (D < DIFF))
        {
                DIFF=D;
                X=$1;
                SET=1;
        }
}

END {   printf("Closest was %s\n", X);  }' < data

This User Gave Thanks to Corona688 For This Post:
# 4  
Old 08-16-2011
I just put the logic in for printing out 30, based on user input of 39, work on the logic and you can get it to print 48 or whatever the next number is. Off course the script needs improvement in terms of error checking, checking if user did supply $1 etc., left as an exercise for OP Smilie. It will go up and down 100 numbers only, which can be tweaked.



Code:
# cat /tmp/23.sh
#!/usr/bin/bash
>/tmp/results
IFS=" ,"
OFS="\n"
v=$1
for i in `cat /tmp/90`
do
for x in {0..100}
do
t=`echo "$v-$x"|bc`
if [[ $t = $i ]]
then
echo $i >> /tmp/results
fi
done
done
echo "`tail -1 /tmp/results`"

here is the running:

# /tmp/23.sh 9
6


# /tmp/23.sh 39
30
# 5  
Old 08-16-2011
That is a useless use of cat. If the file is too large it may silently op off the end bit before feeding it into your program. Since you're using BASH, you can just do:

Code:
while read -d "," V
do
        ...
done < filename

This User Gave Thanks to Corona688 For This Post:
# 6  
Old 08-16-2011
Quote:
Originally Posted by Corona688
Code:
 awk -v VAL=5 -v RS="," '{
        D=(VAL - $1) * (VAL - $1);
        if((!SET) || (D < DIFF))
        {
                DIFF=D;
                X=$1;
                SET=1;
        }
}

END {   printf("Closest was %s\n", X);  }' < data



thanks everyone. this code here does it. but i have a question, the code provides the closest number that PRECEDED the user provided number of 39.

Can it also provide the closest number that comes AFTER 39? that would be awesome if this can be done. Smilie

thanks a million to everyone who has responded.
# 7  
Old 08-16-2011
Slight modification of it for numbers after:

Code:
 awk -v VAL=39 -v RS="," '{
        D=(VAL - $1) * (VAL - $1);
        if((VAL<=$1)&&(((!SET) || (D < DIFF))))
        {
                DIFF=D;
                X=$1;
                SET=1;
        }
}

END {   if(!SET)        print "No greater value found";
        else            printf("Closest was %s\n", X);  }' < data

---------- Post updated at 11:52 AM ---------- Previous update was at 11:46 AM ----------

This will do both:

Code:
awk -v VAL=39 -v RS="," '{
        D=(VAL - $1) * (VAL - $1);

        if((VAL<=$1)&&(((!SETA) || (D < DIFFA))))
        {
                DIFFA=D;
                XA=$1;
                SETA=1;
        }

        if((VAL>=$1)&&(((!SETB) || (D < DIFFB))))
        {
                DIFFB=D;
                XB=$1;
                SETB=1;
        }
}

END {   if(!SETA)       print "No greater bound";
        else            printf("Closest greater bound was %s\n", XA);

        if(!SETB)       print "No lesser bound";
        else            printf("Closest lesser bound was %s\n", XB);   }' < data

It finds 48 and 30 respectively.
This User Gave Thanks to Corona688 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check/print missing number in a consecutive range and remove duplicate numbers

Hi, In an ideal scenario, I will have a listing of db transaction log that gets copied to a DR site and if I have them all, they will be numbered consecutively like below. 1_79811_01234567.arc 1_79812_01234567.arc 1_79813_01234567.arc 1_79814_01234567.arc 1_79815_01234567.arc... (3 Replies)
Discussion started by: newbie_01
3 Replies

2. Shell Programming and Scripting

Print line when closest number if found

so i have a code that identifies which value is the closest to a value provided by the user. awk -F"," -v c=${COLUMN} -v t=${USTIME} '{a=$c}END{ asort(a);d=a-t;d=d<0?-d:d;v = a for(i=NR-1;i>=1;i--){ m=a-t;m=m<0?-m:m if(m<d){ ... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. UNIX for Dummies Questions & Answers

Grep for a range of numbers?

I am trying to extract specific information from a large *.sam file (it's originally 28Gb). I want to extract all lines that are on chr3 somewhere in the range of 112,937,439-113,437,438. Here is a sample line from my file so you can get a feel for what each line looks like: seq.4 0 ... (8 Replies)
Discussion started by: genGirl23
8 Replies

4. Shell Programming and Scripting

grep for a range of numbers

Dear Friends, I want to know how to grep for the lines that has a number between given range(start and end). I have tried the following sed command. sed -n -e '/20030101011442/,/20030101035519/p' However this requires both start and end to be part of the content being grepped. However... (4 Replies)
Discussion started by: tamil.pamaran
4 Replies

5. Shell Programming and Scripting

the smallest number from 90% of highest numbers from all numbers in file

Hello All, I am having problem to find what is the smallest number from 90% of highest numbers from all numbers in file. I am having file with thousands of lines and hundreds of columns. I am familiar mainly with bash but I am open to whatever suggestion witch will lead to the solutions. If I... (11 Replies)
Discussion started by: Apfik
11 Replies

6. UNIX for Dummies Questions & Answers

Frequency of a range of numbers

Hello, I have a column where there are values from 1 to 150. I want to get the frequency of values in the following ranges: 1-5 6-10 11-15 .... .... .... 146-150 How can I do this in a for loop? Thanks, Guss (1 Reply)
Discussion started by: Gussifinknottle
1 Replies

7. Shell Programming and Scripting

delete rows between closest pattern or range

Hi I am having some problom deleting the lines between two specific lines in a file. need to delete lines between two closest lines. i.e need to find the closest range or pattern in a file with repeating patterns. Sample Input: WARNING <some text in n number of lines> ERROR:2597... (10 Replies)
Discussion started by: sudheer1984
10 Replies

8. Shell Programming and Scripting

read numbers from file and output which numbers belongs to which range

Howdy experts, We have some ranges of number which belongs to particual group as below. GroupNo StartRange EndRange Group0125 935300 935399 Group2006 935400 935476 937430 937459 Group0324 935477 935549 ... (6 Replies)
Discussion started by: thepurple
6 Replies

9. Shell Programming and Scripting

awk or sed for finding closest pattern to a line number

hi guys, I want to do pattern matching with awk or sed but I don't know how. here's what I want: I have a line number for a pattern that I have already found using grep, and I know a pattern like "---" that happens a few lines above that certain line number. I want to print out the chunk... (1 Reply)
Discussion started by: alirezan
1 Replies

10. Shell Programming and Scripting

grep numbers range

I want to grep a range of numbers in a log file. My log file looks like this: 20050807070609Z;blah blah That is a combination of yr,month,date,hours,minutes,seconds. I want to search in the log file events that happened between a particular time. like between 20050807070000 to 20050822070000... (1 Reply)
Discussion started by: azmathshaikh
1 Replies
Login or Register to Ask a Question