join two files based on two columns
Believe it or not, I've searched all over the internet and haven't found a working solution for this problem in AWK.
I have two files, A and B:
File A:
chr1 pos1
chr1 pos2
chr2 pos1
chr2 pos2
File B:
chr1 pos1
chr2 pos1
chr3 pos2
Desired Output:
chr1 pos1
chr2 pos1
I'd like to join these two files to basically get the intersection between the two files based on the first AND second columns, not just the first. Since this is the case, most simple scripts won't work and join doesn't seem to be a开发者_运维技巧n option.
Any ideas?
EDIT: sorry, I didn't mention that there are more columns than just the two I showed. I've only shown two in my example because I'm only interested in the first two columns between both files being identical, the rest of the data aren't important (but are nonetheless in the file)
Hum, my idea is the following:
Use join
to merge the two files and correct with awk
$ join A B
chr1 pos1 pos1
chr1 pos2 pos1
chr2 pos1 pos1
chr2 pos2 pos1
$ join A B | awk '{ if ($2==$3) printf("%s %s\n", $1, $2) }'
chr1 pos1 pos1
chr2 pos1 pos1
Edit: given the edit, the join solution may still work (with options), so the concept remains correct (imo).
The awk solution is:
awk 'FILENAME==ARGV[1] {pair[$1 " " $2]; next} ($1 " " $2 in pair)' fileB fileA
Place the smaller file first since you have to basically hold it in memory.
I would write it like this:
awk 'NR == FNR {
k[$1, $2]
next
}
($1, $2) in k
' filea fileb
The order of the input files might need to be adapted based on the exact requirement.
Why not simple grep -f
like this:
grep -f fileB fileA
EDIT:
For files having more than 2 columns try this:
grep "$(cut -d" " -f1,2 fileB)" fileA | cut -d" " -f1,2
精彩评论