Sum up a column of numbers in a text file from the command line with Datamash
TL;DR: sum column 2 where columns are separated by a space:
$ datamash sum 2 -W < file.txt
I have a file like this:
item1 7.50
item2 20
item3 50
item4 300
Note that column 1 and column 2 are separated by a space.
I want to sum all entries from the second column e.g. 7.50
… So I run:
$ datamash sum 2 -W < file.txt
377.50
If you separate columns with tab, you can omit the option -W
.
If Datamash outputs an error, check that your file has the expected number of columns. For example, for a file with 2 columns, you can run the following check:
$ cat file.txt | datamash check column 2 && echo ok || echo fail
If you are not sure what Datamash reads:
$ datamash check < file.txt
4 lines, 1 field
Datamash only “sees” one field (one column). That is because the file I use in this example separates columns (fields) with a space. Don’t forget the option -W
if columns are separated by a space:
$ datamash check -W < file.txt
4 lines, 2 fields
Source: the manpage of Datamash. To display the manpage of Datamash, run $ man datamash
.
personal computing command-line interface (cli) gnu linux trisquel shell literacy office applications wiki datamash spreadsheet stuff in the terminal text processing