(Video materials in preparation)
csum : Calculate cumulative sum
Usage : csum [+r] [+<n>h] [key=<key>] val=<val> <file>
Version : Thu May 22 19:59:12 JST 2014
Adds the specified field (val=) from each row of the speficied file (or
standard input) and inserts the cumulative sum of all previous rows as a new
field immediately to the right of the summed field. This is used to
return a cumulative sum.
Calculates the cumulative total inventory delivered for each store.
(Summs the value in the 4th field and inserts the result in the
5th field.)
(Original Data:data) Store_Name Date Qty Sold Qty_Stocked
$ cat data
Store_A Day_1 103 62
Store_A Day_2 157 94
Store_A Day_3 62 30
Store_A Day_4 131 84
Store_A Day_5 189 111
Store_A Day_6 350 20
Store_A Day_7 412 301
$ csum val=4 data
Store_A Day_1 103 62 62
Store_A Day_2 157 94 156
Store_A Day_3 62 30 186
Store_A Day_4 131 84 270
Store_A Day_5 189 111 381
Store_A Day_6 350 20 401
Store_A Day_7 412 301 702
↑Cumulative sum of field 4 in each record (Cumulative Sum)
If you want to reset the cumulative sum when the value of a particular
field changes, speficiy the reset trigger field as "key=".
Calculates the cumulative total inventory delivered for each store.
(Reset the cumulative sum whenever the value of the 1st field changes.)
(Original Data:data) Store_Name Date Qty Sold Qty_Stocked
$ cat data
Store_A Day_1 103 62
Store_A Day_2 157 94
Store_A Day_3 62 30
Store_A Day_4 131 84
Store_B Day_1 210 113
Store_B Day_2 237 121
Store_B Day_3 150 82
Store_B Day_4 198 105
Store_C Day_1 81 52
Store_C Day_2 76 49
Store_C Day_3 38 21
Store_C Day_4 81 48
$ csum key=1 val=4 data
Store_A Day_1 103 62 62
Store_A Day_2 157 94 156
Store_A Day_3 62 30 186
Store_A Day_4 131 84 270
Store_B Day_1 210 113 113 <- Reset the sum
Store_B Day_2 237 121 234
Store_B Day_3 150 82 316
Store_B Day_4 198 105 421
Store_C Day_1 81 52 52 <- Reset the sum
Store_C Day_2 76 49 101
Store_C Day_3 38 21 122
Store_C Day_4 81 48 170
If you want to replace the target field with the cumulative sum,
use the "+r" option.
Replace the Qty_Stocked with the Cumulative Qty Stocked.
(Cumulative sum the 4th field and output result to the
4th field.)
(Original Data:data) Store_Name Date Qty Sold Qty_Stocked
$ cat data
Store_A Day_1 103 62
Store_A Day_2 157 94
Store_A Day_3 62 30
Store_A Day_4 131 84
Store_A Day_5 189 111
Store_A Day_6 350 20
Store_A Day_7 412 301
$ csum +r val=4 data
Store_A Day_1 103 62
Store_A Day_2 157 156
Store_A Day_3 62 186
Store_A Day_4 131 270
Store_A Day_5 189 381
Store_A Day_6 350 401
Store_A Day_7 412 702
↑Replace with the cumulative sum
The "+<n>h" option will skip the first <n> records in the file. This is useful
when the first line is a header line. The header for the cumulative total
field that is inserted is "@".
(Original Data:data)
$ cat data
Store_Name Date Qty Sold Qty_Stocked <- Header Line
Store_A Day_1 103 62
Store_A Day_2 157 94
Store_A Day_3 62 30
Store_A Day_4 131 84
Store_A Day_5 189 111
Store_A Day_6 350 20
Store_A Day_7 412 301
$ csum +h val=4 data
Store_Name Date Qty Sold @ Qty_Stocked <- "@" is inserted at added field
Store_A Day_1 103 62 62
Store_A Day_2 157 94 156
Store_A Day_3 62 30 186
Store_A Day_4 131 84 270
Store_A Day_5 189 111 381
Store_A Day_6 350 20 401
Store_A Day_7 412 301 702
$ csum +r +h val=4 data
Store_Name Date Qty @ Qty_Stocked <- "Sold" is replaced by "@"
Store_Name Date Qty @ Qty_Stocked <- "Sold" is replaced by "@"
Store_A Day_1 103 62
Store_A Day_2 157 156
Store_A Day_3 62 186
Store_A Day_4 131 270
Store_A Day_5 189 381
Store_A Day_6 350 401
Store_A Day_7 412 702
If the data has decimals, the result is displayed with precision
equal to the most precise value summed.
$ cat data
A 1
A 1.2
A 1.23
B 2
B 2.34
B 3
$ csum key=1 val=2 data
A 1 1
A 1.2 2.2
A 1.23 3.43
B 2 2
B 2.34 4.34
B 3 7.34
val=<pos> can be specified as an exact value, a range (/), multiple
non-consecutive values (@) or relative to the last field (NF NF-<n>).
$ csum val=1 data
$ csum val=2/4 data <-- Sums all fields from the 2nd to the 4th and
inserts the cumulative sum to the right of
each field.
$ csum val=2@4 data <-- Sums the 2nd and 4th fields, and
inserts the cumulative sum to the right of
each field.
$ csum val=NF data <-- You can also specify the last field (NF)
$ csum val=NF-2 data
$ cat data
A 1 2
A 3 4
A 5 6
$ csum val=2/3 data
A 1 1 2 2
A 3 4 4 6
A 5 9 6 12
(Note)
The old syntax is csum [ref=<ref>] key=<key> <file>
This old syntax will be supported for the near future.