0

如果右侧数据很常见,我想合并左侧数据。输入 :

DAN|ab
NAB|ab
PAT|ab
DAN|asd
DAN|gg
NAB|re
DAN|rr
PAT|rr
NAB|sd
DAN|we
PAT|ww

输出应该是:

DAN,NAB,PAT|ab
DAN|asd
DAN|gg
NAB|re
DAN,PAT|rr
NAB|sd
DAN|we
PAT|ww

如果管道的右侧数据很常见,则合并左侧数据。请建议我任何解决方案。

4

3 回答 3

1

另一个:

awk -F\| '{A[$2]=A[$2] (A[$2]?",":x) $1} END{for(i in A)print A[i] FS i}' file
于 2013-02-22T07:18:54.130 回答
1
awk -F"|" '{a[$2]=a[$2]","$1;next}END{for(i in a)print substr(a[i]"|"i,2)}' your_file

测试:

> cat temp
DAN|ab
NAB|ab
PAT|ab
DAN|asd
DAN|gg
NAB|re
DAN|rr
PAT|rr
NAB|sd
DAN|we
PAT|ww
> awk -F"|" '{a[$2]=a[$2]","$1;next}END{for(i in a)print substr(a[i]"|"i,2)}' temp
DAN|gg
DAN|asd
NAB|sd
NAB|re
DAN|we
DAN,PAT|rr
PAT|ww
DAN,NAB,PAT|ab
于 2013-02-22T06:05:26.150 回答
0
awk '
  BEGIN {FS="|"}
  {if (vals[$2]) { vals[$2]=vals[$2]","$1 } else { vals[$2]=$1 } }
  END { for (key in vals) { print vals[key]"|"key } }
'
于 2013-02-22T05:59:30.420 回答