awk - how to get difference of the same column when other column matches

I have a file like this :

# cat list
cucm, location,76,2
cucm1,location1,76,4
cucm,location,80,8
cucm1,location1,90,8
cucm1,location1,87,11
cucm,location,67,9

and I want output like this :

cucm,location,76,2
cucm1,location1,76,4
cucm,location,80, 6 ===> (8-2 =6)
cucm1,location1,90,4 ====>(8-4=4)
cucm1,location1,87,3
cucm,location,67,1

Only when the first and second column matches, I should take a difference of fourth column in the consecutive matching rows.

How will I achieve it. ?

I tried this :

awk -F"," '/cucm1,location1/ {print $1, $2, $3, $4, $4-prev;prev=$4;}' list

This works. But i may not know the contents in the runtime. Hence it has to check if column1 and column2 matches and then do a diff.

Please help

Please use

CODE tags first
awk -F"," '/cucm1,location1/ {print $1, $2, $3, $4, $4-prev;prev=$4;}' list

Does not seem to match what happens in the output. Too hard to understand what you are trying to do.

Assuming the space before the 2nd column in the input file(line 1) is a typo:

awk -F, '{x=$4;$4-=a[$1$2];a[$1$2]=x;}1'  OFS=, file

If not a typo, use this:

awk -F, '{gsub(/^ */,"",$2);x=$4;$4-=a[$1$2];a[$1$2]=x;}1'  OFS=, file

Guru.

Thanks a lot, Guru.
It works.
Thanks again.

Can you please explain your code snippet ?

---------- Post updated at 04:20 AM ---------- Previous update was at 03:54 AM ----------

How will I do if i have to find out the delta of multiple columns.
Thanks in Advance