【发布时间】:2014-06-01 23:57:36
【问题描述】:
我有一个格式为 (ip,info) 的文件 localhost.txt:
192.168.1.8 host name alex
192.168.1.8 macaddress 1354321313
192.168.1.8 time 04:18
192.168.1.20 date 6/2/2012
192.168.1.20 host name riko
192.168.1.11 year 2014
192.168.1.11 host name cr7
192.168.1.11 shared files off
192.168.1.11 time 12:84
我希望输出结果为:
192.168.1.8 host name alex
192.168.1.8 macaddress 1354321313
192.168.1.8 time 04:18
192.168.1.20 date 6/2/2012
192.168.1.20 host name riko
192.168.1.11 year 2014
192.168.1.11 host name cr7
192.168.1.11 shared files off
192.168.1.11 time 12:84
我试过了
awk '{if(NR > 1 && $2 != prev_two){printf "\n";} prev_two=$2; print $0}' localhost.txt >> result.txt
还有这个
awk 'prev != $2 {print ""} {print; prev=$2}' localhost.txt >> result.txt
但是没用
我读过这个
【问题讨论】: