seq 1 10 | awk '{s+=$1} END {print s}'

Category: Data Science & Analysis

seq 1 10 | awk '{s+=$1} END {print s}'

Sum a stream of numbers with awk

awk processes input line by line: the pattern {s+=$1} accumulates the first field of every line into s, and the special END block prints the total after the last line. The pipe symbol (|) feeds seq's output into awk's input. This is the canonical awk accumulator pattern. It works on any whitespace-separated stream, not just seq output.
Looking for more? Search all 7,657 commands — works offline, in English or Spanish, and fixes typos.