天天看点

awk文件比较条件输出

file1

chr2        intron          25           30          -        m

chr2        intron          46           71          -        m

chr2        intron          79           90          -        m

chr2        intron          107           130          -        m

chr3        intron          258           271          -        n

chr3        intron          279           290          -        n

chr3        intron          307           330          -        n

file2

chr2        cds        36          98          -        m

chr3        cds        246          295          -        n

首先file1中的($1 和$6) 和 file2中的 ($1 and $6)相同, 之后 其每行的file1中的($3 和$4)若处于file2($3 and $4)的范围中,则打印出来。

期望得到的结果:

chr2        intron          46           71          -        m

chr2        intron          79           90          -        m

chr3        intron          258           271          -        n

chr3        intron          279           290          -        n

处理方法:

awk ‘NR==FNR{a[$1,$6]=$3” ”$4}NR!=FNR&&a[$1,$6]{split(a[$1,$6],t)if($3>t[1]&&$4<t[2])print $0}’  file2 file1

解析:

先对file2按照第一个字段和最后一个字段为数组进行统计,之后读取第二个文件,如果存在,并且符合在两个数字范围的话进行输出。

awk