How to append two files with common field.

I have two files like

File1 : will get this file from "who" command. It is a unix file.

user val1 Jul 29 13:15 (IP Address1)
user val3 Jul 30 03:21 (IP Address2)
user val2 Jul 29 13:16 (IP Address3)
user val4 Jul 29 13:17 (IP Address4)

File2 : I will export the data from DATABASE with space as a delimiter. So this file is different from unix file

val1 id1 machine name
val2 id2 machine name
val4 id3 machine name
val3 id4 machine name

Question is:

I want to append 1st column of the first file to 2nd file by comparing (val1,val3,val2,val4) in the 1st file with (val1,val2,val4,val3) second file.

Please, Can anyone help in this.

Hint:

loop thru' FIle 2
Get the first column value in a variable var1
look for var1 in col2 of FIle1 #using grep may be
when you get the line, take the first column value
print the line of file2 with the user name appended

Please show the the required result from your sample datas.

Jean-Pierre.

Output file should be like this

val1 id1 machine name user (IP Address1)
val2 id2 machine name user (IP Address2)
val4 id3 machine name user (IP Address3)
val3 id4 machine name user (IP Address4)

An awk statement below should work :

awk 'BEGIN {OFS=" "} NR==FNR { a[$1]=$0 ;next} { for ( i in a) { if ($2==i) { print a,$1,$6,$7} } }' file2 file1