Need help to understand Awk code.

Hi Guys,
Can someone please explain this code to me. I could figure out it's adding and comparing two fields but I am not sure which ones.

sort -t"|" -k3.1 /tmp/mpcashqc.xtr| awk -F"|" '{CHECKAMT[$2]+=$3;BATCHTOT[$2]=$4;\
items[$2]++}END{for(i in CHECKAMT) if (CHECKAMT!=BATCHTOT) printf("%s|%0.2f|%0.2f|%s \n"),i,CHECKAMT,\
BATCHTOT,items}'|sort >/tmp/tmpcashqc.btot.errors

Following is the format of mpcashqc.xtr

20110406|M095800|168.8|15419.43|135|IS00001006|20110406
20110406|M095800|168.8|15419.43|135|IS00001006|20110406
20110406|M095001|159.9|36135.04|249|IS00001153|20110406
20110406|M095016|35.9|35365.46|250|IS00002230|20110406
20110406|M095010|126.2|33721.09|249|IS00002494|20110406
20110406|M095017|159.9|35033.71|250|IS00002763|20110406
20110406|M095007|116|35818.03|250|IS00002947|20110406
20110406|M095025|92|11609.16|78|IS00002962|20110406

For example let's consider the value M095800 in the field $2

For that value it will sum all the value in field $3 and if the final sum of fields $3 differs from the value in field $4 (15419.43)

It will log information in an error log file

If fact it will do that for each and every possible values that appears in the field $2

The information logged in the log file will be
1) The Value of the fields $2 for which a difference has been found between it's sum of $3 and the value of it's fields $4 (not the sum of the fields $4)
2) the sum found for the field $3
3) the field $4
4) the number of lines whose fields $2 has the value reported in 1)

Awk will do that for each an every distinct values of $2 (not only M095800, i just took this value as an example)

Wow! This is a great code!
Do you think the code is wrong somewhere, since it is reporting data even where the sum is correct.
Or
Any other better way to do it.

Thanks for all the help!

if the file mpcashqc.xtr has been clean or truncated or if the fields $4 is not up to date, it may log errors, i have not clue about where the values do come from.

i suppose the format of the file mpcashqc.xtr you have posted is just an extract and not the whole file ...

The script need to be run over an entire and consistent file to have a reliable meaning

No worries.I am too having a learning curve here.The problem here is , it is reporting the data even for the fields where the sum is correct, whereas it should ideally do it only where the sum is incorrect.

1) On which plateform are you ?
if running SunOS / Solaris, use "nawk" instead of "awk"
2) You can upload your file of data so people could do some run test on it and try to reproduce the problem.
3) You may also try to go through a temporary file and make a stand alone awk run over it to make it independant from the commands chain.

So basically what the code means: sort lines of mpcashqc.xtr by the third field with "|" as separator. and pipe it to awk. Awk takes it (again with "|" field separator) and makes a sum of field 3 based on the item number in field 2 (sums up field 3 for item numbers that exist multiple times which yields a checkamt), then it indexes field 4 (batchtotal) by item number (field 2 again), and it makes a count of how many times each item number exists (stored in an array called index). Then, if a discrepancy between the found item sum (checkamt) and the batchtotal is found, it prints out the item number, the checksum total, the expected batch total, and the number that item is found (and added up in checksum total). Then it sorts by item number and the results to an error log file named tmpcashqc.btot.errors.