Bash command to sum a column of numbers

Using existing file:

paste -sd+ infile | bc

Using stdin:

<cmd> | paste -sd+ | bc

Edit: With some paste implementations you need to be more explicit when reading from stdin:

<cmd> | paste -sd+ - | bc

Options used:

-s (serial) – merges all the lines into a single line

-d – use a non-default delimiter (the character + in this case)

Leave a Comment