(Video materials in preparation)
tagsm2 : Sum values in a tag file by key
Usage : tagsm2 <k1> <k2> [<s1> <s2>]
tagsm2 [key=<key>] [val=<val>]
Options : +count
--tagname <tag>
-e
-s<c>
Version : Tue Jan 9 09:02:34 JST 2024
Edition : 1
Sums each field of records that have the same key value within
<file>. The range of key fields begins with "k1" and ends with "k2",
and every field in the range from "s1" to "s2" is summed. Records
with the same key are summed in a single line and output.
<key> 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.
If you specify ":r" as comparison method after the field position,
the fields are compared in reverse order. If you specify ":n" as
comparison method after the field position, that field's values
will be compared as numbers. If you specify ":nr" as comparison
method after the field, the values will be compared in reverse order
as numbers. If you specify comparison method before or after the "/",
you must use the same comparison method for both fields.
TAGa:n/TAGb:n OK
TAGa:n/TAGb:nr Error
TAGa:n/TAGb:r Error
When you specify ":e" as comparison method or specify -e ootion and
no method, characters in the field are replaced as follows and
compared as string:
_ ==> 0x20 (space)
\0 ==> 0x00 (null)
\t ==> 0x09 (tab stop)
\n ==> 0x0a (new line)
\r ==> 0x0d (carrige return)
\_ ==> 0x5f (underscore)
\\ ==> 0x5c (back slash)
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. When comparison
method is attached to brace enclosed tag name, ":" should be ommited.
{TAGa}n/{TAGb}n
<val> is same as <key> except no comparison method is allowed.
Fields not specified as key or sum fields are not output.
Sold_Qty per Item, Store and Day
$ fcols data
No Shop Date A B C D E
0001 ChelseaShop 20060201 91 59 20 76 54
0001 ChelseaShop 20060202 46 39 8 5 21
0001 ChelseaShop 20060203 82 0 23 84 10
0002 SohoShop 20060201 30 50 71 36 30
0002 SohoShop 20060202 78 13 44 28 51
0002 SohoShop 20060203 58 71 20 10 6
0003 MidtownShop 20060201 82 79 16 21 80
0003 MidtownShop 20060202 50 2 33 15 62
0003 MidtownShop 20060203 52 91 44 9 0
0004 TribecaShop 20060201 60 89 33 18 6
0004 TribecaShop 20060202 95 60 35 93 76
0004 TribecaShop 20060203 92 56 83 96 75
Output the quantity sold for each shop.
The key fields are from field "No" to "Shop" and the sum fields are
from "A" to "E".
$ tagsm2 No Shop A E data | fcols
No Shop A B C D E
0001 ChelseaShop 219 98 51 165 85
0002 SohoShop 166 134 135 74 87
0003 MidtownShop 184 172 93 45 142
0004 TribecaShop 247 205 151 207 157
The "+count" option also inserts the total number of records that
match each key immediately after the key field.
$ fcols data2
KEY DATA
1111 3
1111 5
1111 2
2222 3
2222 10
3333 4
3333 8
3333 9
3333 6
$ tagsm2 +count KEY KEY DATA DATA data2 | fcols
KEY * DATA
1111 3 10
2222 2 13
3333 4 27
You can change the tag of the line count field by --tagname option.
$ tagsm2 +count --tagname LINES KEY KEY DATA DATA data2 | fcols
KEY LINES DATA
1111 3 10
2222 2 13
3333 4 27
If there are decimal points in the values, tagsm2 will output all
results to the highest precision found in each key and field summed.
Decimal precision is up to 18 decimal places, for a total of
36-digit precision including the integer part.
$ fcols -- data3
KEY DATA1 DATA2
a 1.4 2.55
a 2 4
b 1.33 2.1
b 5.222 3.12
$ tagsm2 KEY KEY DATA1 DATA2 data3 | fcols --
KEY DATA1 DATA2
a 3.4 6.55
b 6.552 5.22
If you specify 0 0 for the key, tagsm2 outputs the grand total of the
sum fields.
$ fcols -- data4
KEY DATA
a 1
b 2
c 3
$ tagsm2 0 0 DATA DATA data4
DATA
6