Matching column search in two files

Hi,

I have a tab delimited file1:

NC_013499.1    3180    3269    GQ342961.1      
NC_030295.1    5925    6014    FN398100.2      
NC_007915.1    6307    6396    KU529284.1        
NC_013499.1    5033    5122    GQ342961.1

And a second file2:

NC_030295.1     RefSeq  gene    136     5115    .       +       .
NC_007915.1     RefSeq  CDS     6227    7596    .       +       0
NC_030295.1     RefSeq  sequence_feature        1050    1074    .
NC_030295.1     RefSeq  sequence_feature        1533    1557    .       +       .
NC_030295.1     RefSeq  gene    5520    7733    .       +       0

I want to use print combine lines of both files where column 1 of file1 matches column 1 of file2 and column 2 of file1 is >= column4 of file2 and column 3 of file1 is <= column 5 of file2.

My expected output in the example is

NC_030295.1    5925    6014    FN398100.2         NC_030295.1     RefSeq  gene    5520    7733    .       +       0
NC_007915.1    6307    6396    KU529284.1          NC_007915.1     RefSeq  CDS     6227    7596    .       +       0

Your help is well appreciated.

Any attemps / ideas from your side ?

You might want to check out the bottom of this page for releated discussions.

Regards
Peasant.

I tried this but does not give me the desired result

awk '{if (NR==FNR) {l[NR]=$0;a[NR]=$2;b[NR]=$3} else if (a[FNR]>=$4 && b[FNR]<=$5) {print l[FNR],$0}}' file1 file2 . file3 

Try:-

awk -F'\t' '
        NR == FNR {
                A[$1] = $0
                next
        }
        $1 in A {
                split(A[$1], T)
                if ( T[2] >= $4 && T[3] <= $5 )
                        print A[$1], $0
        }
' OFS='\t' file1 file2

The code did no give any output

I suppose then your input is not tab delimited, try:-

awk '
        NR == FNR {
                A[$1] = $0
                next
        }
        $1 in A {
                split(A[$1], T)
                if ( T[2] >= $4 && T[3] <= $5 )
                        print A[$1], $0
        }
'  file1 file2
1 Like

Thanks Yoda. Woks perfectly

Moderator comments were removed during original forum migration.