add up numbers in a file

Sum a column of numbers, from a file or a command's output.

  • text

Steps

  1. awk '{s+=$1} END {print s}' numbers.txt

    the sum of the first column

  2. awk -F, '{s+=$3} END {print s}' data.csv

    the sum of the third column of a CSV

  3. paste -sd+ numbers.txt | bc

    one number a line, added with bc

  4. awk '{s+=$1; n++} END {print s/n}' numbers.txt

    the average

Commands used

  • awk
  • paste
  • bc

Each has a page in lore: lore awk opens it.