- 论坛徽章:
- 145
|
本帖最后由 jason680 于 2014-03-19 11:15 编辑
回复 5# jason680
There is simple way if you don't care about the data sequence
# awk -F: 'NF>1{if(a[$1]==""){a[$1]=$2}else{a[$1]=a[$1]" "$2}}END{for(n in a)print n":"a[n]}' FILE
again: 666
a b c: 111 123 234
new: 555
aaa: 222 123 234
abc: 222 123 234
a a a: 111 123 234
--------------------------------------------------------------
# awk -F: '{
printf("%-15s#NR=%d,NF=%d,$1=%s,$2=%s, before: a[%s]=%s\n",$0,NR,NF,$1,$2,$1,a[$1])}
NF>1{if(a[$1]==""){a[$1]=$2;b[++c]=$1}else{a[$1]=a[$1]" "$2}}
END{for(n=1;n<=c;n++)print b[n]":"a[b[n]]}' FILE
abc: 222 #NR=1,NF=2,$1=abc,$2= 222, before: a[abc]=
a b c: 111 #NR=2,NF=2,$1=a b c,$2= 111, before: a[a b c]=
aaa: 222 #NR=3,NF=2,$1=aaa,$2= 222, before: a[aaa]=
a a a: 111 #NR=4,NF=2,$1=a a a,$2= 111, before: a[a a a]=
#NR=5,NF=0,$1=,$2=, before: a[]=
abc: 123 #NR=6,NF=2,$1=abc,$2= 123, before: a[abc]= 222
a b c: 123 #NR=7,NF=2,$1=a b c,$2= 123, before: a[a b c]= 111
aaa: 123 #NR=8,NF=2,$1=aaa,$2= 123, before: a[aaa]= 222
a a a: 123 #NR=9,NF=2,$1=a a a,$2= 123, before: a[a a a]= 111
new: 555 #NR=10,NF=2,$1=new,$2= 555, before: a[new]=
#NR=11,NF=0,$1=,$2=, before: a[]=
abc: 234 #NR=12,NF=2,$1=abc,$2= 234, before: a[abc]= 222 123
a b c: 234 #NR=13,NF=2,$1=a b c,$2= 234, before: a[a b c]= 111 123
aaa: 234 #NR=14,NF=2,$1=aaa,$2= 234, before: a[aaa]= 222 123
a a a: 234 #NR=15,NF=2,$1=a a a,$2= 234, before: a[a a a]= 111 123
again: 666 #NR=16,NF=2,$1=again,$2= 666, before: a[again]=
abc: 222 123 234
a b c: 111 123 234
aaa: 222 123 234
a a a: 111 123 234
new: 555
again: 666
|
|