13

Help needed on Associative array in awk

view full story
linux-howto

http://www.unix.com – Hi All, I got stuck up with shell script where i use awk. The scenario which i am working on is as below. I have a file text.txt with contents Code: COL1 COL2 COL3 COL4 1 A 500 200 1 B 500 300 2 A 290 150 2 B 290 140 3 C 100 100 I could able to sum col 3 and col4 based on col1 using associative array as below. Code: awk '{a[$1]+=$3;b[$1]+=$4;c[$1]++} END {for (i in c) {print i,a[i]/c[i],b[i]/c[i]}}' Test.txt But, all i need is to get the average on col3 based on col1 and col4 based on col2. I need the result like the below. Code: COL1 COL2 COL3 1 500 500 2 290 290 3 100 100 (HowTos)