如果右侧数据很常见,我想合并左侧数据。输入 :
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
如果管道的右侧数据很常见,则合并左侧数据。请建议我任何解决方案。
另一个:
awk -F\| '{A[$2]=A[$2] (A[$2]?",":x) $1} END{for(i in A)print A[i] FS i}' file
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
awk '
BEGIN {FS="|"}
{if (vals[$2]) { vals[$2]=vals[$2]","$1 } else { vals[$2]=$1 } }
END { for (key in vals) { print vals[key]"|"key } }
'