使用bash连接两个文件的最简单方法及其两个键都出现在结果中 [英] The simplest way to join 2 files using bash and both of their keys appear in the result

查看:102
本文介绍了使用bash连接两个文件的最简单方法及其两个键都出现在结果中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个输入文件

file1

A   0.01
B   0.09
D   0.05
F   0.08

file2

A   0.03
C   0.01
D   0.04
E   0.09

我想要的输出是

A   0.01    0.03
B   0.09    NULL
C   NULL    0.01
D   0.05    0.04
E   NULL    0.09
F   0.08    NULL

我能做的最好的是

join -t'    ' -a 1 -a 2 -1 1 -2 1 -o 1.1,1.2,2.2 file1 file2

没有给我我想要的东西

推荐答案

您可以编写:

join -t $'\t' -a 1 -a 2 -1 1 -2 1 -e NULL -o 0,1.2,2.2 file1 file2

我做了这些更改的地方:

where I've made these changes:

  • 在输出格式中,我将1.1(文件#1的第一列")更改为0(连接字段"),以便在必要时可以将文件#2中的值显示在第一个字段中. (具体来说,这样CE都会.)
  • 我添加了-e选项,以为缺失/空字段指定一个值(NULL).
  • 我使用了$'\t',Bash将其转换为选项卡,而不是键入实际的选项卡.我发现此命令比命令中间的选项卡更易于使用.但是,如果您不同意并且实际的选项卡对您有用,那么您可以继续使用它. :-)
  • In the output format, I changed 1.1 ("first column of file #1") to 0 ("join field"), so that values from file #2 can show up in the first field when necessary. (Specifically, so that C and E will.)
  • I added the -e option to specify a value (NULL) for missing/empty fields.
  • I used $'\t', which Bash converts to a tab, instead of typing an actual tab. I find this easier to use than a tab in the middle of the command. But if you disagree, and the actual tab is working for you, then by all means, you can keep using it. :-)

这篇关于使用bash连接两个文件的最简单方法及其两个键都出现在结果中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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