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