(Video materials in preparation)
tagsm5 : Output the grand total
Usage : tagsm5 <d1> <d2> [<s1> <s2>] <file>
tagsm5 [dummy=<dummy>] [val=<val>] <file>
Option : -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 <file>.
The range of fields from tag <d1> to tag <d2> are dummy fields, and
every field in the range from tag <s1> to <f2> are summed fields. A
record is added at the end of the list containing the grand totals
of all summed fields. The dummy fields in the grand total record are
padded with "@". When a file that has subtotal lines from the
tagsm4 command is processed, the subtotal lines that were added with
tagsm4 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 "-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 Various Cities
$ fcols data
K1 N1 K2 N2 V1 V2 V3 V4 V5
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 a grand total
$ tagsm5 K1 N2 V1 V5 data | fcols
K1 N1 K2 N2 V1 V2 V3 V4 V5
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 "tagsm4".
$ fcols data2
K1 N1 K2 N2 V1 V2 V3 V4 V5
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
$ tagsm5 K1 N2 V1 V5 data2 | fcols
K1 N1 K2 N2 V1 V2 V3 V4 V5
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
K1 N1 K2 N2 V1 V2 V3 V4 V5
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 a grand total
$ tagsm5 -d+++ K1 N2 V1 V5 data3 | fcols
K1 N1 K2 N2 V1 V2 V3 V4 V5
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 "tagsm" 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 "tagsm2", "tagsm4" and "tagsm5" in stages.
cat data |
tagsm2 K1 K6 V1 V5 | <- Total based on key
tagsm4 K1 K4 K5 K6 V1 V5 | <- Sub-subtotal
tagsm4 K1 K2 K3 K6 V1 V5 | <- Subtotal
tagsm5 K1 K6 V1 V5 > result <- Grand total