AWK how to count sum

awk

input file (FileInput.txt):

10
20 3
100 5 3
27

this is my awk script:

BEGIN{
while((getline line < "FileInput.txt") > 0) {


}

how can i count sum? I tried sum+=line however it sums only the first column.

Best Answer

Something like can do the work:

awk 'BEGIN {sum=0} {for (i = 1; i <= NF; i++) sum+=$i} END {print sum}' FileInput.txt
Related Question