在bash中,如何变换multimap< K,V>.到< K,{V1,V2}>的映射 [英] In bash how to transform multimap<K,V> to a map of <K, {V1,V2}>

查看:55
本文介绍了在bash中,如何变换multimap< K,V>.到< K,{V1,V2}>的映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理bash中文件的输出,需要按其键对值进行分组.

I am processing output from a file in bash and need to group values by their keys.

例如,我有

13,47099
13,54024
13,1
13,39956
13,0
17,126223
17,52782
17,4
17,62617
17,0
23,1022724
23,79958
23,80590
23,230
23,1
23,118224
23,0
23,1049
42,72470
42,80185
42,2
42,89199
42,0
54,70344
54,72824
54,1
54,62969
54,1

在文件中,并将特定键中的所有值分组为一行,如

in a file and group all values from a particular key into a single line as in

13,47099,54024,1,39956,0
17,126223,52782,4,62617,0
23,1022724,79958,80590,230,1,118224,0,1049
42,72470,80185,2,89199,0
54,70344,72824,1,62969,1

我的输入文件中大约有10000个条目.如何在Shell中转换此数据?

There are about 10000 entries in my input file. How do I transform this data in shell ?

推荐答案

awk 进行救援!

假设键是连续的...

assuming keys are contiguous...

$ awk -F, 'p!=$1 {if(a) print a; a=p=$1} 
                 {a=a FS $2} 
           END   {print a}' file

13,47099,54024,1,39956,0                                                                                                                  
17,126223,52782,4,62617,0                                                                                                                 
23,1022724,79958,80590,230,1,118224,0,1049                                                                                                
42,72470,80185,2,89199,0                                                                                                                  
54,70344,72824,1,62969,1    

这篇关于在bash中,如何变换multimap< K,V>.到< K,{V1,V2}>的映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆