add up numbers in a file
Sum a column of numbers, from a file or a command's output.
Steps
-
awk '{s+=$1} END {print s}' numbers.txtthe sum of the first column
-
awk -F, '{s+=$3} END {print s}' data.csvthe sum of the third column of a CSV
-
paste -sd+ numbers.txt | bcone number a line, added with bc
-
awk '{s+=$1; n++} END {print s/n}' numbers.txtthe average
Commands used
awkpastebc
Each has a page in lore: lore awk opens it.
Related recipes
- compare two foldersFind which files differ between two folders, or are only in one of them.
- count lines of codeHow many lines of code a project has, in total and by language.
- count lines, words and filesCount the lines of a file or of a command's output, the words, and the files in a folder.
- dates and timestampsPrint dates in any format, turn epoch seconds into dates and back, use other time zones.