Sum values per group (of a text/data file) from the command line with Datamash
We will use Datamash (copyleft-licensed) to sum values per group given a text (data) file. You might need to download it from the repository of the distribution you use. It was not installed on the distribution I use.
Sum values per group
Once you have Datamash installed.
This is the content of the file we want to process:
$ cat example.txt
To sum all amounts by name, run:
$ datamash -WHs -g name sum amount < example.txt
The above command tells Datamash to sum values in the amount field by name. The options passed instruct Datamash to use white spaces as field delimiters, use headers as command input and print those, and sort the input before grouping.
The command should print:
$ man datamash for more examples.
$ datamash check < file.txt if you have issues with printing the expecting output.
$ datamash check < example.txt
6 lines, 2 fields
You can also do pivot table with Datamash by using the option
Datamash was written by Assaf Gordon.
I do self-funded research and I'm writing a book.
> What's the book about?
About technologies and agency.
Meaning, technologies can foster agency. No doubt. But I am also asking:
Can usage of technologies give us a sense of empowerment while in fact undermining our abilities?
I posted a summary of the prologue on the homepage: https://yctct.com/