awk:查询两个文件 [英] awk: two files are queried

查看:119
本文介绍了awk:查询两个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件

file1:

>string1<TAB>Name1
>string2<TAB>Name2
>string3<TAB>Name3

file2:

>string1<TAB>sequence1
>string2<TAB>sequence2

我想使用awk比较各个文件的第1列.如果两个文件共享第1列的值,我想先打印file1的第2列,然后打印file2的第2列.例如,对于上述文件,我的预期输出是:

I want to use awk to compare column 1 of respective files. If both files share a column 1 value I want to print column 2 of file1 followed by column 2 of file2. For example, for the above files my expected output is:

Name1<TAB>sequence1
Name2<TAB>sequence2

这是我的代码:

awk 'BEGIN{FS=OFS="\t"} FNR == NR { a[$1] = $1; next } $1 in a { print a[$2], $2 }' file1 file2 >out

但是我唯一得到的是一个空的第一列序列

But the only thing I get is an empty first columnsequence

这里的错误在哪里?

推荐答案

您的分配不正确.

$ awk 'BEGIN   {FS=OFS="\t"}
       NR==FNR {a[$1]=$2; next} 
       $1 in a {print a[$1],$2}' file1 file2

Name1 sequence1
Name2 sequence2

这篇关于awk:查询两个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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