学习一下狼兄的脚本,这个shell脚本写的很规范:
本帖最后由 kwokcn 于 2010-07-23 17:47 编辑
- #!/bin/bash
- # usage : command file1 [file2 file3 ... filen]
- sort -t\. -k1,1n -k2,2n -k3,3n -k4,4n $* | awk '
- # trans ip to number
- function ip2n(ip) { split(ip,a,"."); return a[1]*256^3+a[2]*256^2+a[3]*256+a[4] }
- # trans number to ip
- function n2ip(n) { return int(n/256^3)"."int(n%256^3/256^2)"."int(n%256^2/256)"."int(n%256) }
- # main
- {
- ipmin=ip2n($1)
- ipmax=ip2n($2)
- if (!min) { min=ipmin; max=ipmax; next }
- if (ipmin<=max+1) max=ipmax>max?ipmax:max
- else { print n2ip(min),n2ip(max); min=ipmin; max=ipmax }
- }
- END {
- print n2ip(min),n2ip(max)
- }'
复制代码 |