检查值是否在两个数字之间
check if value is between two numbers
我有一个这样的文件
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
我想知道第 3 列中的每个值是否包含在第 1 列和第 2 列的所有间隔中。
我必须找出第 3 列的值是否包含在前两列的间隔之一中。你有好主意吗?在 awk 中?或 Python?
提前致谢
结果是这样的:
Value 772617 is found between 700000 and 800000
Value 779286 is found between 700000 and 800000
Value 779287 is found between 700000 and 800000
....
目前我使用这个,但是这个解决方案太多余了?
awk -v x=772617 '<x && x< {print x, "is contained between", , "and", }' file > file_result
你可以试试这个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
使用awk
作为:
awk '
NR > 1 {
vals[++n] =
a[] =
}
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
我想到了这个,我通过间隔循环第 3 列的每个实例。这将找到所有案例。
BEGIN{
FS=" "
i=1 }
FNR==NR{
# LOOP FIRST FILE
mi[i]=
mx[i]=
i++
next
}
{
# ---- looping second file
myval=
for (j=2;j<i;j++) {
if ((mi[j]<=myval) && (mx[j]>=myval)) {
print " is found in " mi[j] " .." mx[j]
}
}
}
为此,我使用 FNR==FN
检查来遍历 2 个文件。但在这种情况下,我循环同一个文件两次,首先是获取值,然后是比较。
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
我有一个这样的文件
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
我想知道第 3 列中的每个值是否包含在第 1 列和第 2 列的所有间隔中。 我必须找出第 3 列的值是否包含在前两列的间隔之一中。你有好主意吗?在 awk 中?或 Python? 提前致谢
结果是这样的:
Value 772617 is found between 700000 and 800000
Value 779286 is found between 700000 and 800000
Value 779287 is found between 700000 and 800000
....
目前我使用这个,但是这个解决方案太多余了?
awk -v x=772617 '<x && x< {print x, "is contained between", , "and", }' file > file_result
你可以试试这个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
使用awk
作为:
awk '
NR > 1 {
vals[++n] =
a[] =
}
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
我想到了这个,我通过间隔循环第 3 列的每个实例。这将找到所有案例。
BEGIN{
FS=" "
i=1 }
FNR==NR{
# LOOP FIRST FILE
mi[i]=
mx[i]=
i++
next
}
{
# ---- looping second file
myval=
for (j=2;j<i;j++) {
if ((mi[j]<=myval) && (mx[j]>=myval)) {
print " is found in " mi[j] " .." mx[j]
}
}
}
为此,我使用 FNR==FN
检查来遍历 2 个文件。但在这种情况下,我循环同一个文件两次,首先是获取值,然后是比较。
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