I have two files for which I want to compare first 2 columns. What I need is if col1 & 2 of file 1 matches col 1 & 2 of file 2, print that line of file 2.
File 1:
chr1 1 A
chr1 2 T
chr1 3 C
File 2:
chr1 2 Y
chr1 3 R
chr1 4 Q
chr2 5 R
output:
chr1 4 Q
chr2 5 R
I know in awk I can compare the two columns and print the matching lines in output but somehow the intersection does not work. The command that I use is:
awk 'NR==FNR { a[$1"\t"$2]=$3 } NR>FNR { k=$1"\t"$2; if (k in a) print $0}' file1 file2
File 1:
chr1 1 A
chr1 2 T
chr1 3 C
File 2:
chr1 2 Y
chr1 3 R
chr1 4 Q
chr2 5 R
output:
chr1 4 Q
chr2 5 R
I know in awk I can compare the two columns and print the matching lines in output but somehow the intersection does not work. The command that I use is:
awk 'NR==FNR { a[$1"\t"$2]=$3 } NR>FNR { k=$1"\t"$2; if (k in a) print $0}' file1 file2
Comment