Linux 在unix中按一列合并两个文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9575972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:00:21  来源:igfitidea点击:

Merging two files by a single column in unix

linuxunixmerge

提问by CadisEtRama

I would like to merge two files by one column in unix.

我想在 unix 中按一列合并两个文件。

I have file_a:

我有文件_a:

subjectid name age  
12 Jane 16  
24 Kristen 90  
15 Clarke 78  
23 Joann 31  

I have another file_b:

我还有另一个 file_b:

subjectid prob_disease  
12 0.009  
24 0.738  
15 0.392  
23 1.2E-5  

I would like to merge these files in the command line. I'd like to merge files a and b by subjectid. Since each file is about 2 million lines long, I tried in R but it froze due to the amount of data, could someone please help me do this in linux? Desired output:

我想在命令行中合并这些文件。我想通过subjectid合并文件a和b。由于每个文件长约 200 万行,我在 R 中尝试过,但由于数据量太大,它冻结了,有人可以帮我在 linux 中做到这一点吗?期望的输出:

subjectid prob_disease name age  
12 0.009 Jane 16  
24 0.738 Kristen 90   
15 0.392 Clarke 78  
23 1.2E-5 Joanna 31     

Please help and thank you!

请帮助并感谢您!

采纳答案by Carl Norum

Check out join(1). In your case, you don't even need any flags:

退房join(1)。在您的情况下,您甚至不需要任何标志:

$ join file_b file_a
subjectid prob_disease name age
12 0.009 Jane 16
24 0.738 Kristen 90
15 0.392 Clarke 78
23 1.2E-5 Joann 31

回答by Kevin

You're looking for the joincommand:

您正在寻找以下join命令:

$ cat test.1
12 Jane 16
24 Kristen 90
15 Clarke 78
23 Joann 31 
$ cat test.2
12 0.009
24 0.738
15 0.392
23 1.2E-5 
$ join -j1 -o 2.1,2.2,1.2,1.3  <(sort test.1) <(sort test.2)
12 0.009 Jane 16
15 0.392 Clarke 78
23 1.2E-5 Joann 31
24 0.738 Kristen 90
$