'check if value is between two numbers
I have a file like this
Start End Value
199146 199389 772617
377581 379032 779286
277876 279322 779287
255497 255843 809151
224415 224896 809550
246516 246946 810776
700000 800000 810777
363597 364598 810802
365242 365735 810898
454121 548113
413324 844816
And I would like to know for each value in column three if it is included in all intervals in columns 1 and 2. I have to find out if the value of the 3rd column is included in one of the intervals of the first two columns. Do you have an idea? In awk? or Python? thanks in advance
The result like this :
Value 772617 is found between 700000 and 800000
Value 779286 is found between 700000 and 800000
Value 779287 is found between 700000 and 800000
....
For now I use this but it's too redundant a solution?
awk -v x=772617 '$1<x && x<$2 {print x, "is contained between", $1, "and", $2}' file > file_result
Solution 1:[1]
You may try this awk:
cat file
Start End Value
199146 199389 772617
377581 379032 779286
277876 279322 779287
255497 255843 809151
224415 224896 809550
246516 246946 810776
700000 800000 810777
363597 364598 810802
365242 365735 810898
454121 548113
713324 810716
Use awk as:
awk '
NR > 1 {
vals[++n] = $3
a[$1] = $2
}
END {
for (i=1; i<=n; ++i)
for (k in a)
if (vals[i] >= k && vals[i] <= a[k]) {
print "Value", vals[i], "is found between", k, "and", a[k]
break
}
}' file
Value 772617 is found between 700000 and 800000
Value 779286 is found between 700000 and 800000
Value 779287 is found between 700000 and 800000
Value 809151 is found between 713324 and 810716
Value 809550 is found between 713324 and 810716
Solution 2:[2]
I came up with this, where I loop every instance of column 3 through intervals. This will find all the cases.
BEGIN{
FS=" "
i=1 }
FNR==NR{
# LOOP FIRST FILE
mi[i]=$1
mx[i]=$2
i++
next
}
{
# ---- looping second file
myval=$3
for (j=2;j<i;j++) {
if ((mi[j]<=myval) && (mx[j]>=myval)) {
print $3 " is found in " mi[j] " .." mx[j]
}
}
}
To do this, I use the FNR==FN check to loop over 2 files. But in this case, I loop the same file twice, first to get the values, then to compare.
c:\Temp>awk -f s.awk data.txt data.txt
772617 is found in 700000 ..800000
772617 is found in 413324 ..844816
779286 is found in 700000 ..800000
779286 is found in 413324 ..844816
779287 is found in 700000 ..800000
779287 is found in 413324 ..844816
809151 is found in 413324 ..844816
809550 is found in 413324 ..844816
810776 is found in 413324 ..844816
810777 is found in 413324 ..844816
810802 is found in 413324 ..844816
810898 is found in 413324 ..844816
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | RavinderSingh13 |
| Solution 2 | MyICQ |
