加入2个文件的基础上在一个列字段 [英] join 2 files based on fields in a columns
问题描述
我正尝试加入具有不同的行和列的长度两个文件,并有一个包含在一个无序的方式类似的信息的列。
I´m trying to join two files that have different row and columns length and have a column which contains similar information in an unsorted way.
该文件是这样的:
文件1
The files look like this: file 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
也在寻找其他职位<一个href=\"http://stackoverflow.com/questions/18152187/how-to-join-multiple-txt-files-into-based-on-column\">How为基于列的联合多个txt文件? 加入第一列两个文件,我想通了,最好的办法是使用
Looking also other posts How to join multiple txt files into based on column? Join on first column of two files, I figured out that the best way is using
awk
我试过了:
awk -F "\t" 'NR==FNR{a[$2]=$0; next}$1 in a {print $0, a[$2]}' file2 file1 > file3
的问题是,它打印只是file1和不file2的
The problem is that it prints just file1 and not file2.
我真的不明白的地方我的错误是。任何建议?
I really do not understand where my mistake is. Any suggestion?
非常感谢你!
推荐答案
您没有张贴预期的输出,但你的awk的线路有问题,我想这应该是:
you didn't post the expected output, but your awk line has problem, I guess it should be:
awk -F "\t" 'NR==FNR{a[$2]=$0; next}$1 in a {print $0, a[$1]}' file2 file1
我在最后一个 {...}
, A [$ 2]改变 - &GT;一[$ 1]
。
这篇关于加入2个文件的基础上在一个列字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!