我有一个像这样的文件
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
我想知道第三列中的每个值是否包含在第1列和第2列的所有区间中。我必须找出第三列的值是否包含在前两列的区间中。你有什么主意吗?在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 '$1<x && x<$2 {print x, "is contained between", $1, "and", $2}' 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] = $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
我想出了这个方法,我通过间隔循环第3列的每个实例。这将找到所有的情况。
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]
}
}
}
要做到这一点,我使用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