Group By/Sum from Shell

Linux group by, sum and count

Use another associated array to store frequency of date as in:

awk '{++freq[$2]; sum[$2]+=$1}
END{for (date in sum) print sum[date], date, freq[date]}' file

879216430 2017-10-14 5

Also note key of your array would be $2 i.e. date not $1

How can we sum the values group by from file using shell script

With one awk command:

awk '{a[$2]+=$4} END {for (i in a) print i,a[i]}' file

Output

William 190
John 250

If you want to sort the output, you can pipe to sort, e.g. descending by numerical second field:

awk '{a[$2]+=$4} END {for (i in a) print i,a[i]}' file | sort -rnk2

or ascending by student name:

awk '{a[$2]+=$4} END {for (i in a) print i,a[i]}' file | sort

Group By and Sum from .txt file in Linux

You're only grouping your sum by one column, but you want to group it by 2 columns, so you need to use both of them as the key in the array.

You're also printing a freq array, but you never assigned it. It also doesn't exist in your desired output.

awk '{sum[$2" "$1]+=$4}
END{for (date in sum) print date, sum[date]}' Test_Awk_2.txt | sort -n -k1,2

The sort command will put all the same values of $2 in consecutive rows.

Best way to simulate group by from bash?

sort ip_addresses | uniq -c

This will print the count first, but other than that it should be exactly what you want.



Related Topics



Leave a reply



Submit