Need help to understand Awk code.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help to understand Awk code.
# 1  
Old 04-07-2011
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.

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

Following is the format of mpcashqc.xtr

Code:
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

# 2  
Old 04-07-2011
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)
# 3  
Old 04-07-2011
Thanks a lot!

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!
# 4  
Old 04-07-2011
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
# 5  
Old 04-07-2011
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.
# 6  
Old 04-07-2011
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.
# 7  
Old 04-07-2011
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Help understand awk command

Help understand awk command This command converts the column values to rows. Command: awk -s1=" " '{S=S?S OFS s1 $0 s1:s1 $0 s1} END{print S}' OFS=, Input_file Example: 1 2 3 is converted to: 1, 2, 3 Can anyone please help me understand this command? Please use code tags when... (1 Reply)
Discussion started by: mohan44
1 Replies

2. UNIX for Dummies Questions & Answers

help me to understand this code

Hi guys can you please help me to understand this code . tmpArray=(${line//=/ }) Please next time open a new thread in the appropriate forum and use code tags (6 Replies)
Discussion started by: sandhya.gilla
6 Replies

3. UNIX for Dummies Questions & Answers

Unable to understand awk script.

Hi below is the samll awk script which i am not able to understand properly. here a bit confusion in 2nd line for me 1. why *\ is not used before . in second line as it was used in first line *\$*\. */ 2. why always \ is used in every first line 3. is there any specific meaning... (9 Replies)
Discussion started by: scriptor
9 Replies

4. UNIX for Dummies Questions & Answers

Trying to understand a complex bit of code

Hi, To re-introduce myself, I'm a router guy trying to learn some scripting from the examples in my work place... In a ksh script, one of the script guys wrote the following and I am trying to understand it. I'm hoping someone can explain it to me. The script flow enters a case structure.... (5 Replies)
Discussion started by: Marc G
5 Replies

5. UNIX for Dummies Questions & Answers

AWK - Hard to understand for me

Hi All, I have googled and spent few hours to understand about AWK usage... But still not very much clear.... Upto my understanding 'AWK' deals with files... It reads the file takes it as a input process it and gives output or writes the output to an another file... >> Please correct me... (23 Replies)
Discussion started by: ambarginni
23 Replies

6. Shell Programming and Scripting

help can't understand the code

Hi All, Good day, i'm just wondering what is the meaning of this code? COUNT_EXTRACTED_FILE=`${ECHO_CMD} "${SE_IN_PATH}${SE_IN_FILE}" | ${AWK_CMD} -F"__" '{print $2}'` Thanks, -niks(newbie) (2 Replies)
Discussion started by: nikki1200
2 Replies

7. Shell Programming and Scripting

Don't understand how RS functions in awk

I learn using RS in awk to extract portion of file in this forum which is wonderful solution to the problem. However, I don't understand how exactly it operates. I don't quite understand the mechanism behind how searching for /DATA2/ can result in extracting the whole section under "DATA2" ... (3 Replies)
Discussion started by: joe228
3 Replies

8. UNIX for Dummies Questions & Answers

unable to understand a awk code

i am not able to understand the following code for awk: $awk -F"|" '{ kount++} >END { for (desig in kount) > print desig,kount }' emp.list the input file i.e. emp.list is :: 3432| p.k.agrwal |g.m |sales 4566|g.l.sharma |director|production 3433|r shah | g.m | production... (1 Reply)
Discussion started by: streetfi8er
1 Replies

9. UNIX for Advanced & Expert Users

Code Understand ?

Hey, Could you explain me what this code is doing ? For example: $i has value 9 $1 is the name of the file say FILEBkUp while do if $1.`/bin/expr $i - 1` ] then mv -f $1.`/bin/expr $i - 1` $1.$i fi i=`/bin/expr $i - 1` done Thanks... (2 Replies)
Discussion started by: varungupta
2 Replies
Login or Register to Ask a Question