(Video materials in preparation)
sm5 : Output the grand total
Usage : sm5 <d1> <d2> [<s1> <s2>] <file>
sm5 [dummy=<dummy>] [val=<val>] <file>
Options : +<n>h
-d<string>
Version : Tue Jan 9 09:02:34 JST 2024
Edition : 1
This tool adds a grand total line (total of all lines) to the
<input> file. The range of key fields begins with "k1" and ends
with "k2", and every field in the range from "f1" to "f2" are summed
fields. A record is added at the end of the list containing the
grand totals of all summed fields that are not key fields. The
fields in the grand total row that are key fields are padded with
"@". When a file that has subtotal lines from the sm4 command is
processed, the subtotal lines that were added with sm4 are ignored
when calculating the grand total.
<dummy> and <val> designates the field position as fllows:
single field TAGa TAGa field
contiguous fields TAGa/TAGb from TAGa field to TAGb field
combination TAGa@TAGb TAGa field and TAGb field
There is no limit on the length of the key field or on the number
of key fields. The key field can also contain multi-byte characters
such as Japanese.
The tag name specfying the field can be eclosed by braces {}. In
this case, tag names can include special charcter like "/" or "@".
Moreover, tag names can include pairs of braces.
{TAGa}/{TAGb}
The "+h" option treats the first row as a header row and excludes it
from the sum.
The "-d<string>" option changes the dummy string as <string>. In
this case, the dummy string is inserted once for each dummy field
regardless the dummy field width. Records which have a dummy field
equivalent to <string> is treated as total row.
Survey Data from Each City
$ fcols data
01 Massachusetts 01 Boston 91 59 20 76 54
01 Massachusetts 02 Worcester 46 39 8 5 21
01 Massachusetts 03 Springfield 82 0 23 84 10
02 New_York 04 Manhattan 30 50 71 36 30
02 New_York 05 Brooklyn 78 13 44 28 51
02 New_York 06 Queens 58 71 20 10 6
02 New_York 07 Albany 82 79 16 21 80
02 New_York 08 Buffalo 50 2 33 15 62
03 New_Jersey 09 Newark 52 91 44 9 0
03 New_Jersey 10 Trenton 60 89 33 18 6
03 New_Jersey 11 Moorestown 95 60 35 93 76
04 Pennsylvania 12 Philadelphia 92 56 83 96 75
04 Pennsylvania 13 Pittsburgh 30 12 32 44 19
04 Pennsylvania 14 Lancaster 48 66 23 71 24
Output the grand total.
$ sm5 1 4 5 NF data | fcols
01 Massachusetts 01 Boston 91 59 20 76 54
01 Massachusetts 02 Worcester 46 39 8 5 21
01 Massachusetts 03 Springfield 82 0 23 84 10
02 New_York 04 Manhattan 30 50 71 36 30
02 New_York 05 Brooklyn 78 13 44 28 51
02 New_York 06 Queens 58 71 20 10 6
02 New_York 07 Albany 82 79 16 21 80
02 New_York 08 Buffalo 50 2 33 15 62
03 New_Jersey 09 Newark 52 91 44 9 0
03 New_Jersey 10 Trenton 60 89 33 18 6
03 New_Jersey 11 Moorestown 95 60 35 93 76
04 Pennsylvania 12 Philadelphia 92 56 83 96 75
04 Pennsylvania 13 Pittsburgh 30 12 32 44 19
04 Pennsylvania 14 Lancaster 48 66 23 71 24
@@ @@@@@@@@@@@@@ @@ @@@@@@ 894 687 485 606 514
When processing data that has subtotal lines added by "sm4".
$ fcols data2
01 Massachusetts 01 Boston 91 59 20 76 54
01 Massachusetts 02 Worcester 46 39 8 5 21
01 Massachusetts 03 Springfield 82 0 23 84 10
01 Massachusetts @@ @@@@@@ 219 98 51 165 85
02 New_York 04 Manhattan 30 50 71 36 30
02 New_York 05 Brooklyn 78 13 44 28 51
02 New_York 06 Queens 58 71 20 10 6
02 New_York 07 Albany 82 79 16 21 80
02 New_York 08 Buffalo 50 2 33 15 62
02 New_York @@ @@@@@@ 298 215 184 110 229
03 New_Jersey 09 Newark 52 91 44 9 0
03 New_Jersey 10 Trenton 60 89 33 18 6
03 New_Jersey 11 Moorestown 95 60 35 93 76
03 New_Jersey @@ @@@@@@ 207 240 112 120 82
04 Pennsylvania 12 Philadelphia 92 56 83 96 75
04 Pennsylvania 13 Pittsburgh 30 12 32 44 19
04 Pennsylvania 14 Lancaster 48 66 23 71 24
04 Pennsylvania @@ @@@@@@ 170 134 138 211 118
$ sm5 1 4 5 NF data2 | fcols
01 Massachusetts 01 Boston 91 59 20 76 54
01 Massachusetts 02 Worcester 46 39 8 5 21
01 Massachusetts 03 Springfield 82 0 23 84 10
01 Massachusetts @@ @@@@@@ 219 98 51 165 85
02 New_York 04 Manhattan 30 50 71 36 30
02 New_York 05 Brooklyn 78 13 44 28 51
02 New_York 06 Queens 58 71 20 10 6
02 New_York 07 Albany 82 79 16 21 80
02 New_York 08 Buffalo 50 2 33 15 62
02 New_York @@ @@@@@@ 298 215 184 110 229
03 New_Jersey 09 Newark 52 91 44 9 0
03 New_Jersey 10 Trenton 60 89 33 18 6
03 New_Jersey 11 Moorestown 95 60 35 93 76
03 New_Jersey @@ @@@@@@ 207 240 112 120 82
04 Pennsylvania 12 Philadelphia 92 56 83 96 75
04 Pennsylvania 13 Pittsburgh 30 12 32 44 19
04 Pennsylvania 14 Lancaster 48 66 23 71 24
04 Pennsylvania @@ @@@@@@ 170 134 138 211 118
@@ @@@@@@@@@@@@@ @@ @@@@@@ 894 687 485 606 514
-d<string> option
$ fcols data3
01 Massachusetts 01 Boston 91 59 20 76 54
01 Massachusetts 02 Worcester 46 39 8 5 21
01 Massachusetts 03 Springfield 82 0 23 84 10
02 New_York 04 Manhattan 30 50 71 36 30
02 New_York 05 Brooklyn 78 13 44 28 51
02 New_York 06 Queens 58 71 20 10 6
02 New_York 07 Albany 82 79 16 21 80
02 New_York 08 Buffalo 50 2 33 15 62
03 New_Jersey 09 Newark 52 91 44 9 0
03 New_Jersey 10 Trenton 60 89 33 18 6
03 New_Jersey 11 Moorestown 95 60 35 93 76
04 Pennsylvania 12 Philadelphia 92 56 83 96 75
04 Pennsylvania 13 Pittsburgh 30 12 32 44 19
04 Pennsylvania 14 Lancaster 48 66 23 71 24
Output the grand total.
$ sm5 -d+++ 1 4 5 NF data3 | fcols
01 Massachusetts 01 Boston 91 59 20 76 54
01 Massachusetts 02 Worcester 46 39 8 5 21
01 Massachusetts 03 Springfield 82 0 23 84 10
02 New_York 04 Manhattan 30 50 71 36 30
02 New_York 05 Brooklyn 78 13 44 28 51
02 New_York 06 Queens 58 71 20 10 6
02 New_York 07 Albany 82 79 16 21 80
02 New_York 08 Buffalo 50 2 33 15 62
03 New_Jersey 09 Newark 52 91 44 9 0
03 New_Jersey 10 Trenton 60 89 33 18 6
03 New_Jersey 11 Moorestown 95 60 35 93 76
04 Pennsylvania 12 Philadelphia 92 56 83 96 75
04 Pennsylvania 13 Pittsburgh 30 12 32 44 19
04 Pennsylvania 14 Lancaster 48 66 23 71 24
+++ +++ +++ +++ 894 687 485 606 514
Using the "sm" tools together:
When writing a report that summarizes data in the shell, a very
common pattern is to sub-subtotal and subtotal based on keys, and
than at the end create a grand total line. In this case, you can
use "sm2", "sm4" and "sm5" in stages.
cat data |
sm2 1 6 7 NF | <- Total based on key
sm4 1 4 5 6 7 NF | <- Sub-subtotal
sm4 1 2 3 6 7 NF | <- Subtotal
sm5 1 6 7 NF > result <- Grand total