Join file contents via common field

I have 2 files with a common parm - Jobname

File 1
0507 1202 JOBA
0507 1302 JOBB
0507 1452 JOBC
0507 1552 JOBA
0507 1553 JOBA

File2

JOBA abcdefg server4
JOBB defghij server22
JOBC vwxyz12 server55

I would like to take each line from File1 and match the jobname with the jobname in File 2 and produce File 3 as

0507 1202 JOBA abcdefg server4
0507 1302 JOBB defghij server22
0507 1452 JOBC vwxyz12 server55
0507 1552 JOBA abcdefg server4
0507 1553 JOBA abcdefg server4

Could anyone help please, I'm new to scripting.

#!/bin/ksh

cat file1.txt | while read line
do
JOBNAME=$(echo ${line} | awk '{print $3}')
LINE2=$(grep -w $JOBNAME file2.txt)
echo "$line $LINE2" >> file3.txt
done

I think it 's OK

Got the following result

cat file1.txt | while read line
while: Expression syntax.

from

#!/bin/ksh

cat file1.txt | while read line
do
JOBNAME=$(echo ${line} | awk '{print $3}')
LINE2=$(grep -w $JOBNAME file2.txt)
echo "$line $LINE2" >> file3.txt
done

#!/bin/ksh

cat file1.txt | while read line
do
JOBNAME=$(echo ${line} | awk '{print $3}')
LINE2=$(grep -w $JOBNAME file2.txt | awk '{print $2 " " $3}')
echo "$line $LINE2" >> file3.txt
done

it 's better ...

You have to add the full path and the right name of your file.
For exemple a file in /tmp :
cat /tmp/File1 | while read line
do
JOBNAME=$(echo ${line} | awk '{print $3}')
LINE2=$(grep -w $JOBNAME /tmp/File2 | awk '{print $2 " " $3}')
echo "$line $LINE2" >> /tmp/File3
done

I'm sorry I get the same result with full path specified.

it ' s amaising !

On my server (AIX), I have created 4 files :
File1 with the following content :
0507 1202 JOBA
0507 1302 JOBB
0507 1452 JOBC
0507 1552 JOBA
0507 1553 JOBA

File2 :
JOBA abcdefg server4
JOBB defghij server22
JOBC vwxyz12 server55

My script newscript.ksh (chmod u+x newscript.ksh) :
#!/bin/ksh

cat File1 | while read line
do
JOBNAME=$(echo ${line} | awk '{print $3}')
LINE2=$(grep -w $JOBNAME File2 | awk '{print $2 " " $3}')
echo "$line $LINE2" >> File3
done

I'm running the script $PWD/newscript.ksh

So, I have a new file File3 :
# more File3
0507 1202 JOBA abcdefg server4
0507 1302 JOBB defghij server22
0507 1452 JOBC vwxyz12 server55
0507 1552 JOBA abcdefg server4
0507 1553 JOBA abcdefg server4

It's working fine for me.

sort -k 3 < file1 > file1.sort
sort -k 1 < file2 > file2.sort
join -1 3 -2 1 -o 1.1,1.2,1.3,2.2,2.3 file1.sort file2.sort | sort

Since this has no specific AIX-related relevance I will transfer the thread to the Scripting forum. And since the threads title is not too conclusive i edited that too.

Old thread title: "Help with scripting"

-push-
-edit-

phew, what an exertion! ;-))

bakunin