本人的回复:
awk '
# author :expert1
# last-update:2010-10-18
#
function ip2n(ip) { split(ip,a,"."); return a[1]*256^3+a[2]*256^2+a[3]*256+a[4] }
NR==FNR{t[$1]=$3;b[$1]=$2;next}
{ for (i in t)
if(ip2n($0)>=i &&ip2n($0) <= t[i])
{print $0 " in\t" b[i];break}
}' f2 f1
原文
顺便贴上本人写的二分查找,这2个是同类问题
1。首先sort -t\. -k1,1n -k2,2n -k3,3n -k4,4n ip段 >ip_range
2。
- awk '
-
- #作者:expert1
- function ip2n(ip) { split(ip,a,"."); return a[1]*256^3+a[2]*256^2+a[3]*256+a[4] }
- BEGIN {FS="[-: ]+"}
- NR==FNR {
- fuck[k++] = $0;next }
- {
- start = 0
- end = k - 1
- while(start <= end) {
- mid =int(start+ ((end - start)/2))
- split(fuck[mid], kao)
- if(ip2n($1) < ip2n(kao[1]))
- end = mid-1
- else if(ip2n($1) > ip2n(kao[2]))
- start = mid+1
- else {
- print $0 " in \t\t"fuck[mid]
- break
- }
- }
- }' ip_range ip
阅读(1321) | 评论(0) | 转发(0) |