print lines that have similar columns with multiple delimiters

Viewed 70

I have two files:

file1.txt

dn_id101_400_CT_TC    string1
dn_id111_60_TT_AA    string2

file2.txt

dn_id101_400_XX_XX    diffstring1
dn_id400_40_XY_YX    diffstring2
dn_id111_60_GG_CC    diffstring3

I want to print the lines from file2.txt if the first three elements separated by _ from file1.txt are present in the line in file2.txt. Here is my desired output:

dn_id101_400_XX_XX    diffstring1
dn_id111_60_GG_CC    diffstring3

Is there a way to to do this? Maybe by changing the delimiter of an awk? I'm not sure how to handle multiple delimiters in an awk command. Here's an example of what I'd like to use:

awk -F"\t" 'FNR==NR {a[$1]; next}; $1 in a' file1.txt file2.txt
4 Answers
Related