Thursday, November 17, 2016

Summing numbers in a column

To sum several values in column from file we can use paste command and bash arithmetic expressions

$ echo $(($(paste -s -d'+' nums)))

I've always used tr to glue the values into one line, but the remaining '+' sign must be removed:

$ echo $(($(cat nums | tr '\n' '+' |sed 's/.$//')))

Or with awk:

$ awk '{s+=$1}; END { print s}' nums