linux - 根据列中的字段加入 2 个文件

标签 linux join awk

我正在尝试连接两个具有不同行和列长度的文件,并且有一列以未排序的方式包含相似信息。

文件如下所示: 文件1

ab 23 33 4 55 6 7 8 9
ab 2 3 44 5 6 7 8
ab 3 4 22 7 8 9 9 00 9
ad 2 3 4 5 4 3
ac 456 47 8 9 0 0 hh kk
af 2 dd 5 6 zz 7 8

文件2

data ab 33 4 5
data ad 3 4 5 
data ac 3 4 5 6 7 8 

预期输出:

文件 3

ab 23 33 4 55 6 7 8 9 data ab 33 4 5
ab 2 3 44 5 6 7 8 data ab 33 4 5
ab 3 4 22 7 8 9 9 00 9 data ab 33 4 5
ad 2 3 4 5 4 3 data ad 3 4 5 
ac 456 47 8 9 0 0 hh kk data ac 3 4 5 6 7 8

同时查看其他帖子 How to join multiple txt files into based on column? Join on first column of two files ,我发现最好的方法是使用

awk

我试过了:

awk -F "\t" 'NR==FNR{a[$2]=$0; next}$1 in a {print $0, a[$2]}' file2 file1 > file3

问题是它只打印 file1 而不是 file2。

我真的不明白我的错误在哪里。有什么建议吗?

非常感谢!

最佳答案

你没有发布预期的输出,但是你的 awk 行有问题,我想应该是:

awk -F "\t" 'NR==FNR{a[$2]=$0; next}$1 in a {print $0, a[$1]}' file2 file1

我在上一个 {...} 中更改了 a[$2] -> a[$1]

关于linux - 根据列中的字段加入 2 个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24135990/

相关文章:

mysql - 如何从基于其他 3 个表的表中选择数据?

Mysql加入2个表并选择日期范围之间的最大值和最小值

awk - 如何在 Bourne shell 中将 shell 变量传递给 awk?

android - Eclipse ADT插件在linux上注销

regex - 通过 linux shell 执行文本

mysql - Sphinx 索引器错误 : index 'PhoneNumbers2' : Error writing file '/tmp/MYbP6cIt' (Errcode: 28)

linux - 从本地 Windows PC 和 Linux 传输数据

database - PostgreSQL : how to delete rows of Table1 where category = x (but Category is defined in Table 2)?

mysql - awk 从 csv 创建批量 MySQL 插入

linux - 匹配两个文件