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
How Does Execve Call Dynamic Linker/Loader (Ld-Linux.So.2)
Shell Script Get Ctrl+Z with Trap
Does Cron Expression in Unix/Linux Allow Specifying Exact Start and End Dates
How to Rebuild Rootfs in Buildroot
How to Execute Parallel "For" Loops in Bash
Branch-Specific Configuration File Maintenance with Git
Find Out If File Has Been Modified Within the Last 2 Minutes
Is It Ok (Performance-Wise) to Have Hundreds or Thousands of Files in the Same Linux Directory
How to Run the Cron Job as a User Instead of Root User
Component Based Web Project Directory Layout with Git and Symlinks
On X64 Linux, Differencebetween Syscall, Int 0X80 and Ret to Exit a Program
Compiling 32 Bit Assembler on 64 Bit Ubuntu
Error: Could Not Locate the Bindings File. Tried: #56
Force a Shell Script to Fflush
Node.Js Child Processes Being Killed When Parent Dies
Nasm - Symbol 'Printf' Causes Overflow in R_X86_64_Pc32 Relocation