如何将共享唯一ID的行合并到逗号分隔的表中 [英] how to merge rows that share unique IDs into a comma separated table

查看:62
本文介绍了如何将共享唯一ID的行合并到逗号分隔的表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一些关于如何将共享唯一ID的行合并到用逗号分隔的表中的一些提示.非常感谢Perl,sed或awk中的任何提示.

I would like to ask for some hints in how to merge rows that share unique IDs into a comma separated table. Any hints in Perl, sed or awk are greatly appreciated.

这是我现在的桌子的样子:

This is how the table I have looks now:

protein_id go_id
4102    GO:0003676
4125    GO:0003676
4125    GO:0008270
4139    GO:0008270

这就是我想要转换为的方式:

This is how i would like to be converted to:

protein_id  go_id
4102    GO:0003676
4125    GO:0003676, GO:0008270
4139    GO:0008270

推荐答案

使用数组的Perl哈希...

Using a Perl hash of arrays...

#!/usr/bin/perl
use warnings;
use strict;

my %data;
my $header;

while(<DATA>){
    chomp;

    if ($. == 1){
        $header = $_;
        next;
    }
    push @{ $data{(split)[0]} }, (split)[1];
}

print "$header\n";

for my $k (sort {$a<=>$b} keys %data){

    print "$k\t";
    print join(', ', @{ $data{$k} });
    print "\n";
}

__DATA__
protein_id go_id
4102    GO:0003676
4125    GO:0003676
4125    GO:0008270
4139    GO:0008270

这篇关于如何将共享唯一ID的行合并到逗号分隔的表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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