Compare two files using awk or sed, add values in a column if their previous fields are same

Hi All,

I have two files

file1:

abc,def,ghi,5,jkl,mno
pqr,stu,ghi,10,vwx,xyz
cba,ust,ihg,4,cdu,oqw

file2:

ravi,def,kishore
ramu,ust,krishna
joseph,stu,mike

I need two output files as follows

In my above example, each row in file1 has 6 fields and each row in file2 has 3 fields. I should compare field2 in both the files. If field2 is same in both the files then i should get the third field in file2 as the last field in file1.

output:

abc,def,ghi,5,jkl,mno,kishore
pqr,stu,ghi,10,vwx,xyz,mike
cba,ust,ihg,4,cdu,oqw,krishna

Also, If field3 in file1 is same as field3 in the next line then the field4 should add upto field4 in the next line and i should get a unique output as follows

ouput:

abc,def,ghi,15,jkl,mno
cba,ust,ihg,4,cdu,oqw

can somebody please help me with this as i require it pretty urgently. I am using sun solaris. If we can get the output using sed or awk that would be great. Any other way is also appreciated.

Thanks in advance..........

Hi,

awk -F, -v OFS="," 'NR==FNR{a[$2]=$3;next}a[$2]{print $0,a[$2]}' file2 file1

Thanks a lot for the reply dude its working great.......can you please look into the other requirement

---------- Post updated at 06:18 PM ---------- Previous update was at 02:38 PM ----------

can you explain me the code ripat.......

sort -t"," +1 1.txt >1.txt.bak
sort -t"," +1 2.txt >2.txt.bak
join -t"," -j1 2 -j2 2 -o 1.1 1.2 1.3 1.4 1.5 1.6 2.3  1.txt.bak 2.txt.bak