Add values using awk
Hi,
I have this file: Anni;2 Arna;9 Anni;4 Ally;1 Ally;1 I want the output to be: Anni has: 6 Arna has: 9 Ally has: 2 This is my code so far: awk -F';' { count[$1]++ } END { for (w in count) print w "has: " }' myFile How can I find the sum of $2 for each name? I have tried several for loops and incremental variables, but I doesn't seem to work for me. I appreciate any help! - Helene |
Code:
cat file | sed 's/;/ has: /g' > newfile |
sort file | awk -F\; '($1 != prev && NR != 1) {print prev" has "sum; sum=0} {sum+=$2; prev = $1}END{print $1" has "sum}'
|
awk -F';' '{ count[$1]+=$2 } END { for (w in count) print w " has: "count[w] }' file
|
Thanks for help! I'll use the last one which was the one closest to my way of doing it :study:
|
All times are GMT -5. The time now is 03:46 PM. |