awk sum a column and print that sum on each line of input

You can use:

awk -v xyzzy=$(awk '{sum+=$2}END{print sum}' file.txt)
    '{print $0" "xyzzy}' file.txt

This unfortunately means going through the information twice but, since you have to do it once before you get the total, there's no real way around it. Any solution will involve storing up the information before outputting anything (whether in arrays, or going back to the file).

The inner awk works out the total and the outer awk assigns that to a variable which can be printed along with each line.

Applying that to the file:

a 1
b 2
c 3
d 4
e 5

gives you, because 1 + 2 + 3 + 4 + 5 is equal to 15:

a 1 15
b 2 15
c 3 15
d 4 15
e 5 15

Kind of standard awk way.

$ awk 'FNR==NR{sum+=$2;next}; {print $0,sum}' file.txt{,}
1 12 272
2 18 272
3 45 272
4 5 272
5 71 272
6 96 272
7 13 272
8 12 272

Use arrays

{ 
    a[NR] = $0
    sum += $2
}
END {
    for (x = 1; x <= NR; x++) {
        print a[x], sum
    }
}

Output

$ awk -f s.awk file.txt 
1 12 272
2 18 272
3 45 272
4 5 272
5 71 272
6 96 272
7 13 272
8 12 272

Read more in Gnu Awk Manual

Tags:

Printf

Bash

Awk

Sed