从一个文件中的awk搜索栏,如果两个文件匹配打印列 [英] awk search column from one file, if match print columns from both files

查看:142
本文介绍了从一个文件中的awk搜索栏,如果两个文件匹配打印列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从文件2比较来自文件1和第3栏第1列,如果匹配,然后打印file1中的第一列和文件2的两个第一列。

I'm trying to compare column 1 from file1 and column 3 from file 2, if they match then print the first column from file1 and the two first columns from file2.

这里的每个文件的例子:

here's a sample from each file:

文件1

Cre01.g000100   
Cre01.g000500  
Cre01.g000650  

文件2

chromosome_1    71569  |655|Cre01.g000500|protein_coding|CODING|PAC:26902937|1|1)
chromosome_1    93952  |765|Cre01.g000650|protein_coding|CODING|PAC:26903448|11|1)
chromosome_1    99034  |1027|Cre01.g000100 |protein_coding|CODING|PAC:26903318|9|1)

所需的输出

Cre01.g000100  chromosome_1    99034        
Cre01.g000500  chromosome_1    71569   
Cre01.g000650  chromosome_1    93952

我一直在找那个有几分相似不同的线程,但我似乎无法得到它的两个文件打印的列。这里有一些链接,有些相关的:

I've been looking at various threads that are somewhat similar, but I can't seem to get it to print the columns from both files. Here are some links that are somewhat related:

<一个href=\"http://stackoverflow.com/questions/13117346/awk-compare-2-files-2-fields-different-order-in-the-file-print-or-merge-match?rq=1\">awk比较2档,2场不同的顺序中的文件,打印或合并匹配和不匹配行

<一个href=\"http://stackoverflow.com/questions/14470879/obtain-patterns-from-a-file-compare-to-a-column-of-another-file-print-matching\">Obtain从文件模式,比较到另一个文件的一列,打印匹配行,用awk

<一个href=\"http://stackoverflow.com/questions/18592736/awk-compare-columns-from-two-files-impute-values-of-another-column\">awk从比较两个文件列,推诿另一列值

<一个href=\"http://stackoverflow.com/questions/9936962/obtain-patterns-in-one-file-from-another-using-ack-or-awk-or-better-way-than-gre\">Obtain使用ACK或AWK或者更好的办法在一个文件中的模式从另一个比grep的?

<一个href=\"http://stackoverflow.com/questions/28058805/awk-combine-the-data-from-2-files-and-print-to-3rd-file-if-keys-matched\">Awk - 从2档合并的数据,并打印到文件的第3键是否匹配

我觉得我应该已经能够根据这些线程算起来,但它已经两天我一直在努力的codeS的不同的变化,我还没有得到任何地方。
下面是一些code,我已经用我的文件的尝试:

I feel like I should have been able to figure it out based on these threads, but it's been two days that I've been trying different variations of the codes and I haven't gotten anywhere. Here is some code that I've tried using on my files:

awk 'FNR==NR{a[$3]=$1;next;}{print $0 ($3 in a ? a[$3]:"NA")}' file1 file2

awk 'NR==FNR{ a[$1]; next} ($3 in a) {print $1 $2 a[$1]}' file1 file2

awk 'FNR==NR{a[$1]=$0; next}{print a[$1] $0}' file1 file2

我知道我必须创建一个包含文件1的第一列(或文件2的第3列),然后比较其他文件的临时矩阵。如果有匹配,然后从文件2.打印从文件1和列1和第2列第一列

I know i have to create a temp matrix that contains the first column of file1 (or the 3rd column of file2) then compare it to the other file. If there is a match, then print first column from file1 and column 1 and column 2 from file 2.

感谢您的帮助!

推荐答案

您可以使用此 AWK

awk -F '[| ]+' -v OFS='\t' 'NR==FNR{a[$4]=$1 OFS $2; next}
       $1 in a{print $1, a[$1]}' file2 file1
Cre01.g000100   chromosome_1    99034
Cre01.g000500   chromosome_1    71569
Cre01.g000650   chromosome_1    93952

这篇关于从一个文件中的awk搜索栏,如果两个文件匹配打印列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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