awk将一个文件加载到数组中,对另一个文件进行测试 [英] awk load one file into array, test against another file

查看:52
本文介绍了awk将一个文件加载到数组中,对另一个文件进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件:

seqs.fa:

>seq000007;size=72768;
ACTGTGAG
>seq000010;size=53132;
GTAAGATC
GAATTCTT
>seq00045;size=40321;
ACCCATTT
...  

numbers.txt

numbers.txt

72768
53132

我想要的输出将是第一个文件中与第二个文件中的数字相匹配的行:

my desired output would be the lines from the first file that match a number from the second file:

>seq000007;size=72768;
>seq000010;size=53132;

我尝试使用 awk ,但是它只返回与第一个数字匹配的行:

I attempted to use awk, but it only returns lines matching the first number:

awk -F"\n" -v RS=">" 'NR==FNR{for(i=1;i<=NF;i++) A[$i]; next} END {for (header in A) {if ( match(header,$1) ) {print header}}}'  seqs.fa numbers.txt

seq000007;size=72768;
seq072768;size=1;

为什么awk只循环访问numbers.txt中第一行的"header"数组?而且,如果这是 XY问题,那么有没有更好的方法来完成这个目标?

Why is awk only looping through the "header" array for the first line in numbers.txt? And, if this is an XY problem, is there a better way to accomplish this goal?

推荐答案

在数字文件中修正错字后

after fixing the typo in your numbers file

$ awk -F'=|;' 'NR==FNR{a[$1]; next}; $3 in a' numbers.txt seqs.fa

>seq000007;size=72768;
>seq000010;size=53132;

这篇关于awk将一个文件加载到数组中,对另一个文件进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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