我有多行数据:
TRINITY_GG_428_c0_g1_i1_orf1 PF13499.1 EF_hand_5
TRINITY_GG_428_c0_g1_i1_orf1 PF00036.27 efhand
TRINITY_GG_428_c0_g1_i1_orf1 PF13405.1 EF_hand_4
TRINITY_GG_428_c0_g1_i1_orf1 PF13833.1 EF_hand_6
TRINITY_GG_428_c0_g1_i1_orf1 PF13202.1 EF_hand_3
TRINITY_GG_429_c0_g1_i1_orf1 PF00156.22 Pribosyltran
TRINITY_GG_431_c5_g1_i1_orf1 PF00475.13 IGPD
TRINITY_GG_461_c0_g1_i1_orf1 PF01208.12 URO-D
TRINITY_GG_461_c0_g1_i1_orf1 PF12876.2 Cellulase-like
我要做的是把它们转换成一行:
^{pr2}$匹配线总是相邻的。在
如何在sed/awk/Perl/Python中解决这个问题?在
使用GNU sed:
主要的部分是替换:它检查两行是否以相同的字符串开头(直到第一个空格),如果是,则连接这些行,从第二行删除字符串并用管道替换新行。在
拆分:
^{pr2}$要使用BSD sed执行此操作,我们必须围绕标签拆分命令,并使用
-E
标志而不是-r
:为了更好地衡量,我们可以更仔细地观察一下这种替代:
您可以使用python regex来执行类似的操作
只需在当前行的第一个字段与上一行相同的情况下建立所有行的连接记录,然后在第一个字段的值发生更改时打印它:
或者,如果您的输入行键不是连续的,并且您不关心输出顺序与输入顺序相同,并且您的输入文件足够小,可以将其全部保存在内存中,那么您可以使用哈希方法,建议使用不同的答案:
^{pr2}$相关问题 更多 >
编程相关推荐