Hi, I have two files
cat 1
100 1
110 2
113 4
230 5
334 7
500 8
900 10
I have another file
cat 2
100 200
201 300
301 400
401 500
501 600
601 700
701 800
801 900
901 1000
If $1 in file1 falls in the range of a record in file2, then print that record in file2 with $2 in file1. If there are multiple intersections then sum them up. If there is no value, then print 0. My output would be
100 200 7
201 300 5
301 400 7
401 500 8
501 600 0
601 700 0
701 800 0
801 900 10
901 1000 0
How about this:
awk 'NR==FNR{
C[NR]=$1 " " $2
L[C[NR]]=0
next
}
{
for (t in C) {
split(C[t],v," ")
if($1>=v[1] && $1<=v[2])
L[C[t]]+=$2
}
}
END {
for(i=1;i in C;i++)
print C " " L[C]
}' 2 1
2 Likes
Chubler_XL,
Thanks for your time and a wonderful code.
I have another small issue that is bothering me
If I have the following values in another file
175
180
185
199
220
235
246
278
298
311
333
345
399
412
I would like to consider the minimum value and its nearest lesser 100 and the maximum value and its nearest greater 100 and print blocks in a difference of 100.
So, in the above data, the minimum value is 175, so its nearest less value is 100 and the maximum value is 412 and its greatest near 100 is 500.
My output would be
100 200
201 300
301 400
401 500
This should work:
awk '{
min=$1<min||!min?$1:min
max=$1>max||!max?$1:max
}
END {
s=int(min/100)*100
e=int(max/100)*100+100
print s " " s+100
for(i=s+101;i<e;i+=100)
print i " " i+99
}' infile
1 Like