Bash 连接命令 [英] Bash join command

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

问题描述

Infile1:

1 a
3 c
4 d
6 f

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

The second scenario is tab delimited so I used -t

推荐答案

首先 sort 两个文件.然后使用 join 加入两个文件的第一个字段.如果您想删除空格并将a a 转换为aa,您还需要通过sed 管道输出.如下图所示:

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

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

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