compare 2 files based on columns

Hi Experts,

 Is there a way to compare 2 files by columns and print matching cases.

I have 2 files as below, I want cases where col1 and col2 in f1 matches col1 and col2 in f2 to be printed as output. The separator is space. I want the output to have col1 col2 col 3 from both files printed in same line. Please see below example.

> cat f1
a b 1
c d 3
e f 0
f b 4
h h 0
x y 5

> cat f2

f d 1
h h 0 
f b 4
e f 2
c d 3
a b 1
y x 5
> Desired output
a b 1 a b 1
c d 3 c d 3
e f 0 e f 2
h h 0 h h 0
f b 4 f b 4

Thanks.

Best Regards,
Mani BR.

Try:

awk 'NR==FNR{A[$1,$2]=$0;next} A[$1,$2]{print $0,A[$1,$2]}' f2 f1

Awesome, It worked as a charm.

(1) Also Is it possible to print the non-matching cases from both files

(2) Interest of learning, If possible can you explain the command used.

Thanks.

using ksh/bash/dash/...

> f3
> f1.notf2
cat f1   |   while read a b c
do
        data=$(grep "^$a $b "   f2    2>/dev/null    )
        [ "$data" != "" ] && echo "$a $b $c $data"    >> f3
        [ "$data" = "" ] && echo "$a $b $c "    >> f1.notf2
done

Hi Scrutinizer,

The script still prints common lines only, Can you please comment.
$ cat f1
a b 1
c d 2
e f 3

$ cat f2
e f 3
a b 1
g h 1
$ source awk.csh
a b 1 a b 1
e f 3  e f 3
Desired Output 
c d 2
g h 1

Thanks.

If you do

ls -l *.nomatch 

You should see files that contain the non-matchess... If the files are called f2 and f1 then you should see f1.nomatch and f2.nomatch

I'm such a dud, I missed it earlier.

Yeah it works,

Thanks a lot