justaguy
justaguy

Reputation: 3022

awk to match file1 with file2 and output matches

I am trying to use awk to match file1 with file2 and print the lines that match in a separate file. File1 is ~4MB and I am getting the below error and I can not seem to fix it. Thank you :).

awk 'NR==FNR{c[$0]; next} ($0 in c)' RS="," file1.txt RS="\n" file2.txt > match.txt

awk: program limit exceeded: maximum number of fields size=32767 FILENAME="sort.2.txt" FNR=1 NR=1

File1

chr1:3063265-3063458 AVP:exon.3 8.55959
chr1:947806-947967 RSPO4:exon.3 246.54
chr2:12758246-12758422 CTD-2192J16.22:exon.2;MAN2B1:exon.1;MAN2B1:exon.20;MAN2B1:exon.22 221.483
chr2:57975642-57975745 KIF5A:exon.1;KIF5A:exon.23;KIF5A:exon.26 222.932

File2

AVP
KIF5A

Desired output

chr1:3063265-3063458 AVP:exon.3 8.55959
chr2:57975642-57975745 KIF5A:exon.1;KIF5A:exon.23;KIF5A:exon.26 222.932

Upvotes: 0

Views: 570

Answers (1)

Jose Ricardo Bustos M.
Jose Ricardo Bustos M.

Reputation: 8164

you can try,

awk '
    FNR==NR{d[$0]; next;}          #Store each key to find, from file2
    {                              #for each line in file1
        for(k in d){               #for each key in d (file2)
            pat="(^|;)"k":";       #pattern to search (regular expression)
            if($2 ~ pat){
                print;             #print if match with RE
                break;
            }
        }
    }' file2 file1

you get,

chr1:3063265-3063458 AVP:exon.3 8.55959
chr2:57975642-57975745 KIF5A:exon.1;KIF5A:exon.23;KIF5A:exon.26 222.932

Upvotes: 3

Related Questions