August 27, 2024

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

No affiliate links, no analytics, no tracking, no cookies. This work © 2016-2024 by yctct is licensed under CC BY-ND 4.0 .   about me   contact me   all entries & tags   FAQ   GPG public key

GPG fingerprint: 2E0F FB60 7FEF 11D0 FB45 4DDC E979 E52A 7036 7A88