用awk并行循环两个文件,打印匹配和不匹配

用awk并行循环两个文件,打印匹配和不匹配

我有两个文件,想将它们并行地逐行读入我的命令中。该命令独立运行:

第一个参数在第一列中匹配,然后它必须匹配第 4 列和第 5 列中的两个数字比较,以便从 data.file 打印第 9 列。这实际上是有效的:

awk '$1=="argument1" && $4<"argument2" && $5>"argument3" {print f} {f=$9}' ../data.file >> OUTPUT.txt

1)我想现在运行它,将参数替换为变量并使用来自两个不同文件的输入。像这样的东西:

cat FILE1a FILE2a | while read FILE1 && read FILE2; do
awk -v j="$FILE1" -v i="$FILE2" '$1==j && $4<i && $5>i {print f} {f=$9}' ../data.file >> OUTPUT.txt
done

但由于某种原因我没有得到任何输出。我尝试用“和”编写变量,但我无法在这里得到输出。您能让我知道如何解决这个问题吗?非常感谢。

2)如果参数与类似的内容不匹配,我还想打印“no_match_found”:

awk '$1=="argument1" && $4<"argument2" && $5>"argument3" {print f} {f=$9} END { if (!p) print "No_match_found"}' ../data.file >> OUTPUT.txt 

这似乎适用于该命令,但不仅在未找到匹配项时打印“No_match_found”,而且在实际找到匹配项时也打印“No_match_found”。我有一点困惑。

所以整个命令应该是这样的,但我就是无法让它工作:

cat FILE1a FILE2a | while read FILE1 && read FILE2; do
awk -v j="$FILE1" -v i="$FILE2" '$1==j && $4<i && $5>i {print f} {f=$9} END { if (!p) print "No_match_found"}' ../data.file >> OUTPUT.txt
done

任何帮助将非常感激。

以下是这些文件的示例: 文件1

scaffold100
scaffold100
scaffold100
scaffold110
scaffold127

文件2

20331
63580
82754
80477
114357

数据文件

scaffold100 RGF GHT 267634  268411  .   -   .   ID=scaffold100.13_LINE-1_reverse_transcriptase-like
scaffold100 RGF GHT 269975  271003  .   -   .   ID=scaffold100.14_LINE-1_reverse_transcriptase-like
scaffold100 RGF GHT 272492  272848  .   -   .   ID=scaffold100.15_Pleckstrin-likey_domain-containing_protein_1
scaffold100 RGF GHT 276355  277065  .   -   .   ID=scaffold100.16_NA
scaffold100 RGF GHT 278580  281340  .   -   .   ID=scaffold100.17_putative_RNA_pseudouridine_synthase_YlyB
scaffold100 RGF GHT 296344  308565  .   -   .   ID=scaffold100.18_Outer_membrane_protein_assembly_factor_BamB
scaffold100 RGF GHT 324074  337460  .   -   .   ID=scaffold100.19_hypothetical_protein_AK812_SmicGene20166
scaffold100 RGF GHT 344155  379840  .   -   .   ID=scaffold100.20_Procollagen_galactosyltransferase_1
scaffold100 RGF GHT 392748  402819  .   -   .   ID=scaffold100.21_Alkylated_DNA_repair_protein_alkB-like_8
scaffold100 RGF GHT 404481  415086  .   -   .   ID=scaffold100.22_Voltage-dependent_P/Q-type_calcium_channel_subunit_alpha-1A
scaffold100 RGF GHT 461507  466605  .   -   .   ID=scaffold100.23_hypothetical_protein_AK812_SmicGene32864
scaffold100 RGF GHT 20321   21040   .   -   .   ID=scaffold100.3_Gamma-tubulin_complex_component_4-like
scaffold100 RGF GHT 53560   69701   .   -   .   ID=scaffold100.4_COBW_domain-containing_protein_1
scaffold100 RGF GHT 72734   97123   .   -   .   ID=scaffold100.5_Polycystin-2
scaffold127 RGF GHT 106337  122095  .   -   .   ID=scaffold127.6_Histone-lysine_N-methyltransferase_SMYD3
scaffold127 RGF GHT 155484  177458  .   +   .   ID=scaffold127.9_Tetratricopeptide_repeat

期望的输出:

ID=scaffold100.3_Gamma-tubulin_complex_component_4-like
ID=scaffold100.4_COBW_domain-containing_protein_1
ID=scaffold100.5_Polycystin-2
No_match_found
ID=scaffold127.6_Histone-lysine_N-methyltransferase_SMYD3

答案1

$ cat tst.awk
NR==FNR {
    ++cnt[$1]
    beg[$1,cnt[$1]] = $4
    end[$1,cnt[$1]] = $5
    map[$1,$4,$5] = $9
    next
}
{
    result = "No_match_found"
    for (i=1; i<=cnt[$1]; i++) {
        if ( (beg[$1,i] < $2) && ($2 < end[$1,i]) ) {
            result = map[$1,beg[$1,i],end[$1,i]]
            break
        }
    }
    print result
}

$ paste file1 file2 | awk -f tst.awk data.file -
ID=scaffold100.3_Gamma-tubulin_complex_component_4-like
ID=scaffold100.4_COBW_domain-containing_protein_1
ID=scaffold100.5_Polycystin-2
No_match_found
ID=scaffold127.6_Histone-lysine_N-methyltransferase_SMYD3

相关内容