0

I am new to awk and trying to compare ":" separated columns 1(eg., chr10) and 2 (eg 10000003) between file1 and file 2 ; and write the matching lines into a new file using awk.

File 1:

chr10:10000003
chr10:10000005
chr10:10000015
chr10:10000017
chr10:100000202
chr10:10000033
chr10:100000380
chr10:10000043
chr10:100000465
chr10:10000052

File 2:

chr1:1806476:T/C:   -2.12680332451125   0.835119313863368\
chr1:1806503:A/C:   -1.56871277809939   0.764924263070418\
chr10:10000003:C/T: -0.572267893158369  0.607055146639116\
chr1:1825420:C/T:   1.70588504817348    0.22407517592607\
chr1:2019496:G/C:   2.34709890656509    0.147215274051584\
chr1:2019501:C/T:   -2.06157612494769   0.82769600171016\
chr10:100000202:C/A:    0.808838763489275   0.362093542746135\
chr1:2028192:G/A:   -0.164564659049733  0.534780784989026\
chr1:2029672:C/A:   -1.31298871130864   0.727940863740118\
chr1:2228889:C/G:   -1.570481759004 0.765170049967457\
chr10:100000465:C/T:    -0.701703282910107  0.629368417133545\
chr1:2306256:C/T:   -1.72965371800758   0.786695642291442\

Expected output: The matched lines from file 2 in the same format as file 2 (attached above)

chr10:10000003:C/T: -0.572267893158369  0.607055146639116\
chr10:100000202:C/A:    0.808838763489275   0.362093542746135\
chr10:100000465:C/T:    -0.701703282910107  0.629368417133545\

The commands tried so far:

awk -F":\r" 'NR==FNR{a[$1$2]++;next}{if($1$2 in a){print}}' file1.txt file2.txt > output.txt
awk -F":\r" 'NR==FNR{a[($1$2)]++;next}{if(($1$2) in a){print}}' file1.txt file2.txt > output.txt

Error observed is a blank output.txt

Could you please help me point out the mistake?

Thanks in Advance!

4
  • Change every $1$2 to ($1,$2) since you need a separator to create a unique key from 2 fields (without a separator a bc -> abc and ab c -> abc) but more importantly - there are no matches between file1 and file2 in the example you posted. Every $1 in file1 is chr10 while every $1 in file2 is chr1. Please edit your question to show concise, testable, textual sample input/output. No images and no links - just text we can copy/paste to test with.
    – Ed Morton
    Commented Feb 11, 2020 at 7:18
  • @ Ed Morton: Thanks for your inputs. I have edited the question post. I also tried implementing the recommended solution, the output file is still blank. Could you please have a look.
    – Gul
    Commented Feb 12, 2020 at 0:56
  • Thanks! This worked : awk -F'[:\r]' 'NR==FNR{a[$1];next}($1 in a){print $0}' file1.txt file2.txt
    – Gul
    Commented Feb 12, 2020 at 1:11
  • You misread my comment. I didn't say "change $1$2 to ($1$2)", I said "change $1$2 to ($1,$2)". The , separator is the most important part as explained in my comment. Using -F":\r" makes no sense - think about what that actually means. -F"[:\r]" also makes no sense for what you're trying to do since you don't have any \r-separated fields. The solution you say works for you doesn't at all do what you asked for so idk why you think it works.
    – Ed Morton
    Commented Feb 12, 2020 at 21:21

2 Answers 2

0

You can use below command Let me know for any doubts

awk 'NR==FNR{a[$1];next}($1 in a){print $0}'  file1 file2
1
  • The above command is not working for me. I have edited the question in detail now, could you please give it a shot?
    – Gul
    Commented Feb 12, 2020 at 0:53
0

This is what you really need:

$ awk -F':' 'NR==FNR{a[$1,$2]; next} ($1,$2) in a' file1 file2
chr10:10000003:C/T: -0.572267893158369  0.607055146639116\
chr10:100000202:C/A:    0.808838763489275   0.362093542746135\
chr10:100000465:C/T:    -0.701703282910107  0.629368417133545\

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .