巴什join命令 [英] Bash join command

查看:133
本文介绍了巴什join命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Infile1:

1 a
3 c
4 d
6 f

Infile2:

Infile2:

1 a 
2 b
5 e
6 f
7 g
8 h

我如何加入这些文件与UNIX join命令得到这个输出:

How do I join these files with the unix join command to get this output:

1 aa
2 b
3 c
4 d
5 e
6 ff
7 g 
8 h

Dogbanes答案的工作,但...
当我申请dogbanes该文件回答:

Dogbanes answer worked but... when I apply dogbanes answer on this file:

27  27
28  22
29  37
30  15
31  21
32  13
33  18
34  24

和这样的:

27  7
28  13
29  6
30  12
31  30
32  5
33  10
34  28

他们不加入:

27  27
27  7
28  13
28  22
29  37
29  6
30  12
30  15
31  21
31  30
32  13
32  5
33  10
33  18
34  24
34  28

第二种情况是制表符分隔的,所以我用 -t \\ t

推荐答案

首先排序这两个文件。然后使用加入加入两个文件的第一个字段。如果你想删除的空间,从而转换还需要管道通过 SED 输出 AA AA 。这如下所示:

First sort both files. Then use join to join on the first field of both files. You also need to pipe the output through sed if you want to remove the space and thus convert a a into aa. This is shown below:

$ join -t " " -1 1 -2 1 -a 1 -a 2  <(sort file1) <(sort file2) | sed 's/ \([a-z]\) / \1/g'
1 aa
2 b
3 c
4 d
5 e
6 ff
7 g
8 h

这篇关于巴什join命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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