AWK 合并行

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值