If the line is blank it prints the total and resets it to 0.
Otherwise it adds the 2nd field to the running total.
My data file did not have a blank line at the end, which is the reason I added the END part in the awk script.
If your data file has a blank line at the end, then the last running total is printed on reaching that last blank line and then a 0 total is printed due to that END:
$
$ cat f2
sda 4.80 114.12 128.69 978424 1103384
sdb 0.03 0.40 0.00 3431 0
sda 1.00 0.00 88.00 0 176
sdb 2.00 0.00 0.00 0 0
sda 1.00 0.00 88.00 0 176
sdb 5.60 0.00 0.00 0 0
$
$ awk '{if (/^$/){printf("%s %d => %0.2f\n","Sum",++n,sum); sum=0} else {sum += $2}}END{printf("%s %d => %0.2f\n","Sum",++n,sum)}' f2
Sum 1 => 4.83
Sum 2 => 3.00
Sum 3 => 6.60
Sum 4 => 0.00
$
$
So, either remove the last blank line from your data file or remove the END portion of the script (but not both).
$
$ # 1: Correct => last blank line absent; END portion present
$ cat f2
sda 4.80 114.12 128.69 978424 1103384
sdb 0.03 0.40 0.00 3431 0
sda 1.00 0.00 88.00 0 176
sdb 2.00 0.00 0.00 0 0
sda 1.00 0.00 88.00 0 176
sdb 5.60 0.00 0.00 0 0
$
$ awk '{if (/^$/){printf("%s %d => %0.2f\n","Sum",++n,sum); sum=0} else {sum += $2}}END{printf("%s %d => %0.2f\n","Sum",++n,sum)}' f2
Sum 1 => 4.83
Sum 2 => 3.00
Sum 3 => 6.60
$
$
$ # 2: Correct => last blank line present; END portion absent
$ cat f2
sda 4.80 114.12 128.69 978424 1103384
sdb 0.03 0.40 0.00 3431 0
sda 1.00 0.00 88.00 0 176
sdb 2.00 0.00 0.00 0 0
sda 1.00 0.00 88.00 0 176
sdb 5.60 0.00 0.00 0 0
$
$ awk '{if (/^$/){printf("%s %d => %0.2f\n","Sum",++n,sum); sum=0} else {sum += $2}}' f2
Sum 1 => 4.83
Sum 2 => 3.00
Sum 3 => 6.60
$
$
$ # 3: Incorrect => last blank line absent; END portion absent as well
$ cat f2
sda 4.80 114.12 128.69 978424 1103384
sdb 0.03 0.40 0.00 3431 0
sda 1.00 0.00 88.00 0 176
sdb 2.00 0.00 0.00 0 0
sda 1.00 0.00 88.00 0 176
sdb 5.60 0.00 0.00 0 0
$
$ awk '{if (/^$/){printf("%s %d => %0.2f\n","Sum",++n,sum); sum=0} else {sum += $2}}' f2
Sum 1 => 4.83
Sum 2 => 3.00
$
$
$ # 4: Incorrect => last blank line present; END portion present as well (Your case?)
$ cat f2
sda 4.80 114.12 128.69 978424 1103384
sdb 0.03 0.40 0.00 3431 0
sda 1.00 0.00 88.00 0 176
sdb 2.00 0.00 0.00 0 0
sda 1.00 0.00 88.00 0 176
sdb 5.60 0.00 0.00 0 0
$
$ awk '{if (/^$/){printf("%s %d => %0.2f\n","Sum",++n,sum); sum=0} else {sum += $2}}END{printf("%s %d => %0.2f\n","Sum",++n,sum)}' f2
Sum 1 => 4.83
Sum 2 => 3.00
Sum 3 => 6.60
Sum 4 => 0.00
$
$
tyler_durden