UNIX:基于列的值合并文件 [英] unix: merge files based on column value

查看:121
本文介绍了UNIX:基于列的值合并文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件,​​看起来像这样的:

文件1(2列):

  ID1 123
ID2 234
ID3 232
ID4 344
...

文件2(>一百万个栏)

  ID2 A C ...
ID3克叔...
ID1(C T)...
ID4 A C ...
...

欲从文件1的第2列的基础上对文件2作为第二列中的ID添加的值。因此,合并后的文件应该是这样的:

  ID2 234 A C ...
ID3 232克叔...
ID1 123(C T)...
ID4 344 A C ...
...

所以完全一样文件2(顺序相同的行),但与添加的第2列。该ID是第一列(在这两个文件present)的值。文件1有更多的行/标识不是文件2.所有的文件2 ID是文件1,但不能从文件1的所有ID为文件2。

有谁知道如何在UNIX / bash中做到这一点?非常感谢!


解决方案

  $加入≤(排序文件1)≤(排序文件2)
ID1 123(C T)...
ID2 234 A C ...
ID3 232克叔...
ID4 344 A C ...

如果你想保留文件2

的顺序

  $加入-1 1-2 2';(排序文件1)≤(猫-n文件2 |排序-k2,2)|排序-k3,3n |切-d'-f1-2,4-
ID2 234 A C ...
ID3 232克叔...
ID1 123(C T)...
ID4 344 A C ...

I have two files, that look like this:

File 1 (2 columns):

ID1 123
ID2 234
ID3 232
ID4 344
...

File 2 (>1 million columns)

ID2 A C ...
ID3 G T ...
ID1 C T ...
ID4 A C ... 
...

I want to add the values from column 2 of file 1 based on the ID to file 2 as the second column. So the merged file should look like this:

ID2 234 A C ...
ID3 232 G T ...
ID1 123 C T ...
ID4 344 A C ... 
...

So exactly the same as file 2 (same order of rows), but with the added 2nd column. The IDs are the values of the first column (present in both files). File 1 has more rows/IDs than file 2. All IDs from file 2 are in file 1, but not all IDs from file 1 are in file 2.

Does anyone know how to do this under unix/bash? Many thanks!

解决方案

$ join <(sort file1) <(sort file2)
ID1 123 C T ...
ID2 234 A C ...
ID3 232 G T ...
ID4 344 A C ...

If you want keep the order of file2

$ join -1 1 -2 2 <(sort file1) <(cat -n file2 | sort -k2,2) | sort -k3,3n | cut -d' ' -f1-2,4-
ID2 234 A C ...
ID3 232 G T ...
ID1 123 C T ...
ID4 344 A C ...

这篇关于UNIX:基于列的值合并文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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