Need Help on a "simple" awk

Hello all,

I'm a quite absolute beginner on awk and I will appreciate any help to solve the following problem.

I have two different files, each of these with three columns.
File A (A1, A2, A3) and File B (B1, B2, B3)
The syntax would be as follows:
If A1 == B1 and A2 == B2, then print A1, A2, A3, B4
If not, do nothing

Thank you very in advance for the help you can provide me.
regards

Luca

I couldn't find B4 in your file :slight_smile:
assuming the o/p A1,A2,B1,B2

awk 'NR==FNR{a[$1]=$1;b[$2]=$2;next} $1==a[$1] && $2 == b[$2] { print a[$1],b[$2],$1,$2}' FileA FileB

this is wrong $1 in file B not equal $1 in file A

meaning a[$1] in file A = a[A1] but in file B a[B1]...

awk 'NR==FNR{a[$1]=$1;b[$2]=$2;next} $1==a[$1] && $2 == b[$2] { print a[$1],b[$2],$1,$2}' FileA FileB

correction

awk 'NR==FNR{a[$1]=$1;b[$2]=$2;next} ($1 in a) && ( $2 in b ) { print a[$1],b[$2],$1,$2}' FileA FileB

BR

You are right, sorry!
In fact the output will be A1,A2,A3,B3
Thanks a lot for your hel, I appreciate!

Thanks ahmad. Hadn't thought of that point.

-Anchal.

Hello all,

first of all, thank you very much for your help. In order to print A1, A2, A3 (first three columns from FileA), B3 (third column from File B), I have tried to edit the script modified by ahmad.diab as follows:

NR==FNR{a[$1]=$1;b[$2]=$2;next} ($1 in a) && ( $2 in b ) { print a[$1],b[$2],$3,$3 } FileA FileB

But what I obtained is A1, A2, B3, B3. No way to print A3....

Could someone provide me some help please?

Thank you in advance,
Luca

try:

awk 'NR==FNR{a[$1]=$1;b[$2]=$2;c[$1$2]=$0;next} ($1 in a) && ( $2 in b ) { print c[$1$2],$3}' FileA FileB

I am sure this can be optimized.