Wednesday, February 21, 2007

AWK: Adding salary - Shell Beginner


$ cat sal.txt
Jex Mon clerk 12001
Aji Tue sales 13003
Jex Wed clerk 13123
Salna Thu sales 34000
Aji Mon sales 13123

Adding the Salaries for Jex Salna and Aji

$ awk '{arr[$1]+=$4} END {for (i in arr) {print i,arr[i]}}' sal.txt
Salna 34000
Jex 25124
Aji 26126

And if you want to see how many occurrences of each employee name(first field)

$ awk '{count[$1]++}END{for(j in count) print j,count[j]}' sal.txt
Aji 2
Jex 2
Salna 1

No comments:

© Jadu Saikia www.UNIXCL.com