根据列之间的一致性粘贴两个文件 [英] paste two files according to concordance between columns

查看:113
本文介绍了根据列之间的一致性粘贴两个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助:

我有文件1:

ID
100
102
103
104
108
109
112
.
.
.

和file2:

ID    []    p1    p2
100    2.5    3.0    2.0
101    2.0    4.0    3.0
102    2.6    4.0    2.5
103    2.3    2.0    NA
104    2.3    2.0    2.0
105    3.5    2.8    2.0
106    1.7    NA    3.2
107    5.0    4.0    4.0
108    3.2    2.0    4.0
109    2.9    1.0    1.5
110    5.0    NA    NA
111    2.9    4.0    4.0
112    3.1    2.5    2.0
.
.
.

我想将两个文件都粘贴到file3中,如下所示:

I would like to paste both files in file3 looking like:

ID    []    p1    p2
100    2.5    3.0    2.0
102    2.6    4.0    2.5
103    2.3    2.0    NA
104    2.3    2.0    2.0
108    3.2    2.0    4.0
109    2.9    1.0    1.5
112    3.1    2.5    2.0
.
.
.

基本上,考虑到文件1和文件2中的字段1中的一致性,将数据从文件2粘贴到文件2中的字段2、3、4中.

Basically, pasting data in fields 2,3,4 from file2 to file1, considering concordancies in field1 in both file1 and file2.

我已经用NR == NFR尝试了一些awk命令,但是我得到的输出只有file1的内容,然后是file2的内容...

I've tried some awk commands with NR == NFR but I just get the output with the content of file1 followed by content of file2...

有帮助吗?也欢迎使用带有剪切和粘贴功能的Unix命令

Any help? Unix commands with cut and paste are also welcome

推荐答案

您确实可以为此使用awk:

awk 'NR==FNR{a[$1]=1} NR>FNR && a[$1]' file1 file2

NR==FNR{a[$1]=1} a数组填充了file1的内容.

NR==FNR{a[$1]=1} The a array filled with the content of file1.

NR>FNR && a[$1]将打印file2的行.

NR>FNR && a[$1] is printing the line of file2 if the array contains the ID (aka $1).

这篇关于根据列之间的一致性粘贴两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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