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 1b 2c 3d 4e 5
gives you, because 1 + 2 + 3 + 4 + 5
is equal to 15
:
a 1 15b 2 15c 3 15d 4 15e 5 15
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 2722 18 2723 45 2724 5 2725 71 2726 96 2727 13 2728 12 272
Read more in Gnu Awk Manual