Awk 'value in array'返回匹配,但'print array'输出 OFS 分隔的空白,如何打印数组元素?



我有一些基因组学数据格式:chromosome"t"position"t"feature。每个特性都是一个类的成员,定义在一个引用文件中。我想输出一个格式为class"t"chromosome"t"position"t"feature

的文件

基因组学文件:

$ head *Y.tsv
chrY    8143806 HAL1B
chrY    15923083        LTR25-int
参考文件:

$ head /home/software/RepBase20.05.fasta/humrep_names.ref
HERVH   ERV1    Eutheria
X21_LINE        CR1     Mammalia
代码:

awk '
{FS=OFS="t"}
NR==FNR{a[$1]="";a[$1,1]=$1;a[$1,2]=$2;a[$1,3]=$3; next}
$3 in a{print a[$1,2],$1,$2,$3}
' /home/software/RepBase20.05.fasta/humrep_names.ref *Y.tsv
打印

输出,提示正确读入数组并找到匹配项,但a[$1,2]为空白;输出:

chrY    21596689        L1M2A_5
chrY    16760406        HERV-K14CI
chrY    18692648        MER101_I

为什么用'in'匹配,但是打印没有显示值?如何打印出每个特征(a[$1,1]$3)的类(a[$1,2])?

谢谢!

一定要看一下Ed Morton推荐的那本书,但我认为你的选择或多或少是正确的,只是你在print语句中将$1写在了应该写$3的地方。

$ cat a.awk
# As mentioned in the comments, use BEGIN to only do this once
BEGIN { FS=OFS="t" }
# no change from yours
NR==FNR{ a[$1]=""; a[$1,1]=$1; a[$1,2]=$2; a[$1,3]=$3; next}
# a[$3,2] instead of a[$1,2]
$3 in a {print a[$3,2],$1,$2,$3}
$ cat file1.txt
HERVH   ERV1    Eutheria
X21_LINE        CR1     Mammalia
$ cat file2.txt
chrY    8143806 HAL1B
chrY    15923083        HERVH
$ awk -f a.awk file1.txt file2.txt
ERV1    chrY    15923083        HERVH

最新更新