awk 特征相同的行合并

时间:2022-06-26 16:07:11
[root@linux-node1 ~]# cat test.txt
1001  hisk01
1001  hisk02
1001  hisk03
1002  hisk04
1002  hisk05
1002  hisk06
1003  hisk07
1003  hisk08
[root@linux-node1 ~]# awk'{if($1==x){i=i" "$2}else{if(NR>1){print i};i=$0};x=$1;y=$2}'test.txt <(echo)
1001  hisk01hisk02 hisk03
1002  hisk04hisk05 hisk06
1003  hisk07hisk08
[root@linux-node1 ~]# awk'{a[$1]=a[$1]?a[$1]" "$2:$2}END{for(i=0;i++<asorti(a,b);)printb[i],a[b[i]]}' test.txt 
1001 hisk01 hisk02 hisk03
1002 hisk04 hisk05 hisk06
1003 hisk07 hisk08