Bash Sum values from file or stream

Messing around with a way to sum data values from a plain text file like so:

1
5
 19
22
-9

0

-05
4


Notice that the data file has empty lines

cat <FILENAME> | grep . | paste -sd+ - | bc

Breaking this down it is the following steps

  • cat to read the file contents
  • grep the empty lines out with the "."
  • paste the contents input stdin
  • sum the values with bc

Results in

37

Success!