- 论坛徽章:
- 60
|
本帖最后由 reyleon 于 2013-10-15 14:05 编辑
- [root@centos ~]# cat file
- 192.168.1
- 192.168.11
- 192.168.13
- 192.168.12
- 192.168.14
- 192.168.2
- 192.168.4
- 192.168.3
- 192.168.6
- 172.19.3
- 192.16.1
- 192.16.2
- 10.0.4
- 10.0.3
- 10.0.7
- [root@centos ~]# sort -t. -n -k3 file | awk -f f.awk
- 172.19.3
- 10.0.3-10.0.4
- 10.0.7
- 192.16.1-192.16.2
- 192.168.1-192.168.4
- 192.168.6
- 192.168.11-192.168.14
- [root@centos ~]#
- [root@centos ~]# cat f.awk
- #!/bin/awk
- BEGIN{
- FS=SUBSEP="."
- }
- {
- a[$1,$2]=a[$1,$2]?a[$1,$2]OFS$3:$3
- }
- END{
- for(i in a){
- l=split(a[i],s,OFS)
- for(j=1;j<=l;j++){
- min=s[j]
- while(1){
- if(s[j]+1!=s[++j]){
- max=s[--j]
- if(min==max){print i FS min}else{print i FS min"-"i FS max}
- break
- }
- }
- }
- }
- }
- [root@centos ~]#
复制代码 |
|