My file.txt
looks like this:
1 12
2 18
3 45
4 5
5 71
6 96
7 13
8 12
I can sum the second column like this:
awk '{ sum += $2 } END { print sum }' file.txt
272
What's the neatest way that I can print that sum on each line? This is what I'm expecting:
1 12 272
2 18 272
3 45 272
4 5 272
5 71 272
6 96 272
7 13 272
8 12 272
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
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
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
Offtopic, but in Perl you can do:
perl -nale '$s+=$F[1];push(@a,$_);END{print $_." $s" for(@a);}' file.txt
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With