Friday, 20 November 2015

How to Use Associative Array in AWK

Input File:F_Data_File.txt

1001|A1|10|Y
1002|A2|20|Y
1003|A3|30|N
1004|A4|10|Y
1005|A5|10|N
1006|A6|20|N
1007|A7|20|N
1008|A8|30|N

Desired Output:

Dept_Number==> Num_Of_Emp
----------------------
10 ==>3
30 ==>2
20 ==>3

Solution:

$ awk 'BEGIN{FS="|"; print "Dept_Number==> Num_Of_Emp\n----------------------\n"}
         {V_Dept_Emp_Cnt[$3]++}
         END{for(k in V_Dept_Emp_Cnt) print k,"==>" V_Dept_Emp_Cnt[k]}' F_Data_File.txt
 
Explanation:

1. V_Dept_Emp_Cnt[$3]++ =>  V_Dept_Emp_Cnt[$3] = V_Dept_Emp_Cnt[$3] + 1
2. When first line is read: V_Dept_Emp_Cnt[10] = V_Dept_Emp_Cnt[10] + 1
    value at array index 10 is not available hence,  V_Dept_Emp_Cnt[10] = 0 + 1
    Now: V_Dept_Emp_Cnt[10] will hold 1
3. When next time row with dept number 10 is read it will look like as below:
    V_Dept_Emp_Cnt[10] = V_Dept_Emp_Cnt[10] + 1
    V_Dept_Emp_Cnt[10] = 1 + 1
    V_Dept_Emp_Cnt[10] = 2 and so on.
4. Same process will follow for other dept number.
5. In for loop [for (k in V_Dept_Emp_Cnt) ] k will work as index and will fetch out the value from    array V_Dept_Emp_Cnt. AWK for look internaly understand the index, We can understan it like foloowing ( for (INDEX in ASSOCIATEIVE_ARRAY[INDEX] )
   (10 in V_Dept_Emp_Cnt[10]) => V_Dept_Emp_Cnt[10] = 3
   (20 in V_Dept_Emp_Cnt[20]) => V_Dept_Emp_Cnt[10] = 3
   (30 in V_Dept_Emp_Cnt[30]) => V_Dept_Emp_Cnt[10] = 2

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...