朋友在群里发了一个文本处理的问题,题目如下:
-
# cat a.txt内容如下:
-
192.168.1
-
192.168.2
-
192.168.3
-
172.19.3
-
192.16.1
-
192.16.2
-
10.0.4
-
# 点分割, 前两个字段相同, 第三个字段递增连续的,需要合并成如下结果:
-
192.168.1-192.168.3
-
172.19.3
-
192.16.1-192.16.2
-
10.0.4
各路大侠纷出招:
1、shell脚本实现:
点击(此处)折叠或打开
-
[root@GS1-241 ~]# cat a
-
192.168.1
-
192.168.2
-
192.168.3
-
172.19.3
-
192.16.1
-
192.16.2
-
10.0.4
-
192.16.29
-
192.16.9
-
192.16.99
-
192.16.100
-
[root@GS1-241 ~]# bash a.sh
-
10.0.4
-
172.19.3
-
192.16.1-192.16.2
-
192.16.9
-
192.16.29
-
192.16.99-192.16.100
-
192.168.1-192.168.3
-
[root@GS1-241 ~]# cat a.sh
-
#!/bin/bash
-
-
IFS='.'
-
AB=''
-
C=0
-
tmp=''
-
-
while read a b c
-
do
-
if [[ "$AB" == "$a.$b" && $(( $C + 1)) -eq $c ]];then
-
tmp="$tmp $a.$b.$c"
-
elif [[ "$tmp" == "" ]];then
-
tmp="$a.$b.$c"
-
else
-
tmp="$tmp\n$a.$b.$c"
-
fi
-
AB="$a.$b"
-
C=$c
-
done < <(sort -t. -k 1n -k 3n a)
-
-
echo -e "$tmp" | sed -r "s/ .* | /-/g"
2、sed实现(看哭了)
-
donvan ~ /tmp $ cat test_file
-
192.168.1
-
192.168.2
-
192.168.3
-
172.19.3
-
192.16.1
-
192.16.2
-
10.0.4
-
donvan ~ /tmp $ cut -d '.' -f1,2 test_file |sort -u|xargs -i sed -n ':a;/{}\.[0-9]*$/{s/^/-/;$bb;N;ba;:b;};s/\n[^\n]\{1,\}$//;/^-[^-]/{s/^-//p;q};s/-//g;/^[^\n]*\n[^\n]*$/{s/\n/-/p;q};/\n.*\n/{s/\n.*\n/-/p;q}' test_file
-
10.0.4
-
172.19.3
-
192.16.1-192.16.2
-
192.168.1-192.168.3
3、powershell大牛出招
4、awk实现
-
sort -t. -k 1n -k 3n a.txt | awk -F. '{if(AB==$1"."$2 && C+1==$3){e=$0}else{if(e!=""){print s"-"e}else{print s}; s=$0;e=""}; AB=$1"."$2; C=$3}END{if(e!=""){print s"-"e}else{print s}}'
5、看大伙热情高涨,我也忍不住用python实现了一下
-
>>> b = ['10.0.4', '172.19.3', '192.16.1', '192.16.2', '192.168.29', '192.168.9', '192.168.3', '192.168.4', '192.168.5']
-
>>> c = sorted(b, key=lambda x:(".".join(x.split(".")[:-1]), int(x.split(".")[-1])))
-
>>> a = []
-
>>> for i in range(len(c)):
-
... if not a:a.append(c[i])
-
... if i != len(c) - 1:
-
... if ".".join(c[i].split(".")[:-1]) == ".".join(c[i+1].split(".")[:-1]) and int(c[i].split(".")[-1])+1 == int(c[i+1].split(".")[-1]):continue
-
... if a[0] != c[i]:a.append(c[i])
-
... print "-".join(a)
-
... a = []
-
...
-
10.0.4
-
172.19.3
-
192.16.1-192.16.2
-
192.168.3-192.168.5
-
192.168.9
-
192.168.29
-
>>>
阅读(4383) | 评论(1) | 转发(0) |