root@Scrapy:/tmp# cat test.txt
host_name beijing-192.168.1.104
host_name beijing-192.168.1.105
host_name beijing-192.168.1.103
host_name beijing-192.168.1.250
host_name beijing-192.168.1.252
host_name beijing-192.168.1.251
host_name beijing-192.168.1.122
host_name beijing-192.168.1.123
host_name beijing-192.168.1.132
host_name beijing-192.168.1.133
host_name beijing-192.168.1.152
host_name beijing-192.168.1.153
host_name tianjin-172.168.10.11
host_name tianjin-172.168.10.12
host_name tianjin-172.168.10.13
host_name tianjin-172.168.10.14
host_name tianjin-172.168.10.15
host_name tianjin-172.168.10.19
host_name tianjin-172.168.10.20
host_name liaoning-172.168.10.11
host_name liaoning-172.168.10.12
host_name liaoning-172.168.10.13
host_name liaoning-172.168.10.14
host_name liaoning-172.168.10.15
host_name liaoning-172.168.10.20
host_name liaoning-172.168.10.21
########################################################################################
root@Scrapy:/tmp# awk -F:' |-' '{print $1}' test.txt 判断下分隔符
host_name beijing
host_name beijing
host_name beijing
host_name beijing
host_name beijing
host_name beijing
host_name beijing
host_name beijing
host_name beijing
host_name beijing
host_name beijing
host_name beijing
host_name tianjin
host_name tianjin
host_name tianjin
host_name tianjin
host_name tianjin
host_name tianjin
host_name tianjin
host_name liaoning
host_name liaoning
host_name liaoning
host_name liaoning
host_name liaoning
host_name liaoning
host_name liaoning
root@Scrapy:/tmp# awk -F:' |-' '{a[$1]=a[$1]" "$2}END{for (i in a)print i,a[i]}' test.txt
host_name liaoning 172.168.10.11 172.168.10.12 172.168.10.13 172.168.10.14 172.168.10.15 172.168.10.20 172.168.10.21
host_name tianjin 172.168.10.11 172.168.10.12 172.168.10.13 172.168.10.14 172.168.10.15 172.168.10.19 172.168.10.20
host_name beijing 192.168.1.104 192.168.1.105 192.168.1.103 192.168.1.250 192.168.1.252 192.168.1.251 192.168.1.122
192.168.1.123 192.168.1.132 192.168.1.133 192.168.1.152 192.168.1.153
转载于:https://my.oschina.net/jastme/blog/283324