| 1 |
\input texinfo @c -*-texinfo-*- |
| 2 |
@c %**start of header |
| 3 |
@setfilename datamash.info |
| 4 |
@include version.texi |
| 5 |
@settitle GNU Datamash @value{VERSION} |
| 6 |
|
| 7 |
@c Define a new index for options. |
| 8 |
@defcodeindex op |
| 9 |
@c Combine everything into one index (arbitrarily chosen to be the |
| 10 |
@c concept index). |
| 11 |
@syncodeindex op cp |
| 12 |
@syncodeindex vr cp |
| 13 |
@c %**end of header |
| 14 |
|
| 15 |
@copying |
| 16 |
This manual is for GNU Datamash (version @value{VERSION}, @value{UPDATED}), |
| 17 |
which provides command-line computations on input files. |
| 18 |
|
| 19 |
Copyright @copyright{} 2014--2021 Assaf Gordon. |
| 20 |
Copyright @copyright{} 2022--2025 Timothy Rice. |
| 21 |
|
| 22 |
@quotation |
| 23 |
Permission is granted to copy, distribute and/or modify this document |
| 24 |
under the terms of the GNU Free Documentation License, Version 1.3 or |
| 25 |
any later version published by the Free Software Foundation; with no |
| 26 |
Invariant Sections, no Front-Cover Texts, and no Back-Cover |
| 27 |
Texts. A copy of the license is included in the section entitled |
| 28 |
``GNU Free Documentation License''. |
| 29 |
@end quotation |
| 30 |
@end copying |
| 31 |
@c If your manual is published on paper by the FSF, it should include |
| 32 |
@c the standard FSF Front-Cover and Back-Cover Texts, as given in |
| 33 |
@c maintain.texi. |
| 34 |
|
| 35 |
@dircategory Basics |
| 36 |
@direntry |
| 37 |
* Datamash: (datamash). datamash |
| 38 |
@end direntry |
| 39 |
|
| 40 |
@titlepage |
| 41 |
@title GNU Datamash |
| 42 |
@subtitle for version @value{VERSION}, @value{UPDATED} |
| 43 |
@author GNU Datamash Developers (@email{assafgordon@@gmail.com}) |
| 44 |
@page |
| 45 |
@vskip 0pt plus 1filll |
| 46 |
@insertcopying |
| 47 |
@end titlepage |
| 48 |
|
| 49 |
@contents |
| 50 |
|
| 51 |
|
| 52 |
@ifnottex |
| 53 |
@node Top |
| 54 |
@top Datamash |
| 55 |
|
| 56 |
This manual is for GNU Datamash (version @value{VERSION}, @value{UPDATED}), |
| 57 |
which provides command-line computations on input files. |
| 58 |
@end ifnottex |
| 59 |
|
| 60 |
@menu |
| 61 |
* Overview:: General purpose and information. |
| 62 |
* Invoking datamash:: How to run @command{datamash}. |
| 63 |
* Available Operations:: Available operations in @command{datamash}. |
| 64 |
* Statistical Operations:: Statistical operations in @command{datamash}. |
| 65 |
* Usage Examples:: Usage Examples. |
| 66 |
* Reporting bugs:: Sending bug reports and feature suggestions. |
| 67 |
* GNU Free Documentation License:: Copying and sharing this documentation. |
| 68 |
* Concept index:: Index of concepts. |
| 69 |
|
| 70 |
|
| 71 |
@end menu |
| 72 |
|
| 73 |
@node Overview |
| 74 |
@chapter Overview |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
@cindex overview |
| 79 |
|
| 80 |
The @command{datamash} program |
| 81 |
(@url{https://www.gnu.org/software/datamash}) performs calculation (e.g. |
| 82 |
@emph{sum,}, @emph{count}, @emph{min}, @emph{max}, @emph{skewness}, |
| 83 |
@emph{standard deviation}) on input files. |
| 84 |
|
| 85 |
Example: sum up the values in the first column of the input: |
| 86 |
|
| 87 |
@example |
| 88 |
@cindex example, sum |
| 89 |
$ seq 10 | datamash sum 1 |
| 90 |
55 |
| 91 |
@end example |
| 92 |
|
| 93 |
@command{datamash} can group input data and perform operations on each group. |
| 94 |
It can sort the file, and read header lines. |
| 95 |
|
| 96 |
Example: Given a file with three fields (name, subject, score), |
| 97 |
find the average score in each subject: |
| 98 |
|
| 99 |
@example |
| 100 |
$ cat scores.txt |
| 101 |
Name Subject Score |
| 102 |
Bryan Arts 68 |
| 103 |
Isaiah Arts 80 |
| 104 |
Gabriel Health-Medicine 100 |
| 105 |
Tysza Business 92 |
| 106 |
Zackery Engineering 54 |
| 107 |
... |
| 108 |
|
| 109 |
@cindex sorting |
| 110 |
@cindex grouping |
| 111 |
@cindex example, sorting |
| 112 |
@cindex example, grouping |
| 113 |
$ datamash --sort --headers --group 2 mean 3 sstdev 3 < scores.txt |
| 114 |
GroupBy(Subject) mean(Score) sstdev(Score) |
| 115 |
Arts 68.9474 10.4215 |
| 116 |
Business 87.3636 5.18214 |
| 117 |
Engineering 66.5385 19.8814 |
| 118 |
Health-Medicine 90.6154 9.22441 |
| 119 |
Life-Sciences 55.3333 20.606 |
| 120 |
Social-Sciences 60.2667 17.2273 |
| 121 |
@end example |
| 122 |
|
| 123 |
|
| 124 |
@command{datamash} is designed for interactive exploration of textual data |
| 125 |
and for automating tasks in shell scripts. |
| 126 |
|
| 127 |
@command{datamash} has a rich set of statistical functions to quickly assess |
| 128 |
information in textual input files. An example of calculating basic statistic |
| 129 |
(mean, 1st quartile, median, 3rd quartile, IQR, sample-standard-deviation, |
| 130 |
and p-value of Jarque-Bera test for normal distribution: |
| 131 |
|
| 132 |
@cindex example, statistics |
| 133 |
@example |
| 134 |
$ datamash -H mean 1 q1 1 median 1 q3 1 iqr 1 sstdev 1 jarque 1 < FILE |
| 135 |
mean(x) q1(x) median(x) q3(x) iqr(x) sstdev(x) jarque(x) |
| 136 |
45.32 23 37 61.5 38.5 30.4487 8.0113-09 |
| 137 |
@end example |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
@node Invoking datamash |
| 142 |
@chapter Invoking @command{datamash} |
| 143 |
|
| 144 |
@cindex invoking |
| 145 |
@cindex options |
| 146 |
@cindex usage |
| 147 |
@cindex help |
| 148 |
|
| 149 |
The format for running the @command{datamash} program is: |
| 150 |
|
| 151 |
@example |
| 152 |
datamash [@var{option}]@dots{} @var{op1} @var{column1} @ |
| 153 |
[@var{op2} @var{column2} @dots{}] |
| 154 |
@end example |
| 155 |
|
| 156 |
Where @var{op1} is the operation to perform on the values in @var{column1}. |
| 157 |
@command{datamash} reads input from stdin and performs one or more operations |
| 158 |
on the input data. If @option{--group} is used, each operation is performed |
| 159 |
on every group. If @option{--group} is not used, each operation is performed on |
| 160 |
all the values in the input file. |
| 161 |
|
| 162 |
@vindex LC_NUMERIC |
| 163 |
The @env{LC_NUMERIC} locale specifies the decimal-point character and the |
| 164 |
thousands separator. |
| 165 |
|
| 166 |
@exdent @command{datamash} supports the following operations: |
| 167 |
|
| 168 |
@table @asis |
| 169 |
@item Primary operations: |
| 170 |
@code{groupby}, @code{crosstab}, @code{transpose}, @code{reverse}, |
| 171 |
@code{check} |
| 172 |
|
| 173 |
@item Line-Filtering operations: |
| 174 |
@code{rmdup} |
| 175 |
|
| 176 |
@item Per-Line operations: |
| 177 |
@code{base64}, @code{debase64}, @code{md5}, @code{sha1}, |
| 178 |
@code{sha224}, @code{sha256}, @code{sha384}, @code{sha512}, @code{bin}, |
| 179 |
@code{strbin}, @code{round}, @code{floor}, @code{ceil}, @code{trunc}, |
| 180 |
@code{frac}, @code{dirname}, @code{basename}, @code{extname}, @code{barename}, |
| 181 |
@code{getnum}, @code{cut}, @code{echo} |
| 182 |
|
| 183 |
@item Group-by Numeric operations: |
| 184 |
@code{sum}, @code{min}, @code{max}, @code{absmin}, @code{absmax}, @code{range} |
| 185 |
|
| 186 |
@item Group-by Textual/Numeric operations: |
| 187 |
@code{count}, @code{first}, @code{last}, @code{rand}, |
| 188 |
@code{unique}, @code{uniq}, |
| 189 |
@code{collapse}, @code{countunique} |
| 190 |
|
| 191 |
@item Group-by Statistical operations: |
| 192 |
@code{mean}, @code{geomean}, @code{harmmean}, @code{mode}, |
| 193 |
@code{median}, @code{q1}, @code{q3}, @code{iqr}, @code{perc}, |
| 194 |
@code{antimode}, @code{pstdev}, @code{sstdev}, @code{pvar}, @code{svar}, |
| 195 |
@code{ms}, @code{rms}, @code{mad}, @code{madraw}, @code{sskew}, |
| 196 |
@code{pskew}, @code{skurt}, @code{pkurt}, @code{jarque}, @code{dpo}, |
| 197 |
@code{scov}, @code{pcov}, @code{spearson}, @code{ppearson}, |
| 198 |
@code{dotprod} |
| 199 |
|
| 200 |
@end table |
| 201 |
|
| 202 |
@exdent Grouping options: |
| 203 |
|
| 204 |
@table @option |
| 205 |
@item --skip-comments |
| 206 |
@itemx -C |
| 207 |
@opindex --skip-comments |
| 208 |
@opindex -C |
| 209 |
Skip comment lines (starting with '#' or ';' and optional whitespace). |
| 210 |
|
| 211 |
@item --full |
| 212 |
@itemx -f |
| 213 |
@opindex --full |
| 214 |
@opindex -f |
| 215 |
Print entire input line before op results (default: print only the grouped |
| 216 |
keys). |
| 217 |
While using this option with non-linewise operations was historically permitted, |
| 218 |
it never produced very sensible output. Such usage has been deprecated, and in a |
| 219 |
future release it will result in an error. |
| 220 |
|
| 221 |
@item --group=@var{X[,Y,Z]} |
| 222 |
@itemx -g @var{X[,Y,Z]} |
| 223 |
@opindex --group |
| 224 |
@opindex -g |
| 225 |
@cindex grouping |
| 226 |
Group input via fields @var{X[,Y,Z]}. By default, fields are separated by TABs. |
| 227 |
Use @option{--field-separator} to change the delimiter character. Input file |
| 228 |
must be sorted by the same fields @var{X[,Y,Z]}. Use @option{--sort} |
| 229 |
to automatically sort the input. |
| 230 |
If @option{--group} is not specified, each operation is performed |
| 231 |
in the entire input file. |
| 232 |
Ranges of field numbers like @var{X-Z} are also supported. |
| 233 |
|
| 234 |
@item --header-in |
| 235 |
@opindex --header-in |
| 236 |
Indicates the first input line is column headers, and should not be used for |
| 237 |
any calculations. |
| 238 |
|
| 239 |
@item --header-out |
| 240 |
@opindex --header-out |
| 241 |
Print column headers as first line. If the column header names are known (i.e. |
| 242 |
the input file had a header line, and the @command{command} was invoked with |
| 243 |
@option{--header-in}, @option{-H} or @option{--headers}), prints the operation |
| 244 |
and the name of the field (e.g. @samp{mean(X)}). Otherwise, prints the number |
| 245 |
operation and the field number (e.g. @samp{mean(field-3)}). |
| 246 |
|
| 247 |
@item --headers |
| 248 |
@itemx -H |
| 249 |
@opindex --headers |
| 250 |
@opindex -H |
| 251 |
Same as @samp{--header-in --header-out}. A short option indicating the input |
| 252 |
file has a header line, and the output should contain a header line as well. |
| 253 |
|
| 254 |
@item --vnlog |
| 255 |
@opindex --vnlog |
| 256 |
Enable experimental support for the vnlog data file format for both input |
| 257 |
and output. This format is explained at @url{https://github.com/dkogan/vnlog}. |
| 258 |
|
| 259 |
@item --ignore-case |
| 260 |
@itemx -i |
| 261 |
@opindex --ignore-case |
| 262 |
@opindex -i |
| 263 |
Ignore upper/lower case when comparing text for grouping, sorting, and comparing |
| 264 |
unique values in the @samp{countunique} and @samp{unique} |
| 265 |
(or @samp{uniq}) operations. |
| 266 |
|
| 267 |
@item --sort |
| 268 |
@itemx -s |
| 269 |
@opindex --sort |
| 270 |
@opindex -s |
| 271 |
@cindex sorting |
| 272 |
Sort the input before grouping. @command{datamash} requires sorted input. If |
| 273 |
the input is not sorted, using @option{--sort} will automatically sort the input |
| 274 |
before processing it further. Sorting will be performed based on the specified |
| 275 |
@option{--group} parameter, and respecting case @option{--ignore-case} option |
| 276 |
(if used). The following commands are equivalent: |
| 277 |
@example |
| 278 |
$ cat FILE | sort -k1,1 | datamash --group 1 sum 1 |
| 279 |
$ cat FILE | datamash --sort --group 1 sum 1 |
| 280 |
@end example |
| 281 |
|
| 282 |
@item --sort-cmd=@var{PATH} |
| 283 |
@opindex --sort-cmd |
| 284 |
@cindex sorting |
| 285 |
Use the given program to sort instead of the system @command{sort} |
| 286 |
|
| 287 |
@end table |
| 288 |
|
| 289 |
|
| 290 |
@exdent File Operation options: |
| 291 |
|
| 292 |
@table @option |
| 293 |
|
| 294 |
@item --no-strict |
| 295 |
@opindex --no-strict |
| 296 |
Allow lines with varying number of fields. By default, @option{transpose} and |
| 297 |
@option{reverse} will fail with an error message unless all input lines have |
| 298 |
the same number of fields. |
| 299 |
|
| 300 |
@item --filler=@var{x} |
| 301 |
@opindex --filler |
| 302 |
When use @option{--no-strict} option, missing fields will be filled with this |
| 303 |
value. |
| 304 |
@end table |
| 305 |
|
| 306 |
@exdent General options: |
| 307 |
|
| 308 |
@table @option |
| 309 |
|
| 310 |
@item --format=@var{FORMAT} |
| 311 |
@opindex --format |
| 312 |
print numeric values with printf style floating-point @var{FORMAT}. |
| 313 |
|
| 314 |
|
| 315 |
@item --field-separator=@var{x} |
| 316 |
@itemx -t @var{x} |
| 317 |
@opindex --field-separator |
| 318 |
@opindex -t |
| 319 |
Use character @var{X} instead of TAB as input and output field delimiter. |
| 320 |
If @option{--output-delimiter} is also used, it will override the output |
| 321 |
field delimiter. |
| 322 |
|
| 323 |
@item --narm |
| 324 |
@opindex --narm |
| 325 |
Skip @var{NA} or @var{NaN} values. |
| 326 |
|
| 327 |
@item --output-delimiter=@var{x} |
| 328 |
@opindex --output-delimiter |
| 329 |
@opindex -t |
| 330 |
Use character @var{X} instead as output field delimiter. |
| 331 |
This option overrides @option{--field-separator}/@option{-t}/ |
| 332 |
@option{--whitespace}/@option{-W}. |
| 333 |
|
| 334 |
@item --collapse-delimiter=@var{x} |
| 335 |
@itemx -c @var{x} |
| 336 |
@opindex --collapse-delimiter |
| 337 |
@opindex -c |
| 338 |
|
| 339 |
Use character @var{X} instead of comma to delimit items in a |
| 340 |
@samp{collapse} or @samp{unique} (aka @samp{uniq}) list. |
| 341 |
|
| 342 |
@item --round=@var{N} |
| 343 |
@itemx -R @var{N} |
| 344 |
@opindex --round |
| 345 |
@opindex -R |
| 346 |
Round numeric output to @var{N} decimal places. |
| 347 |
|
| 348 |
@item --whitespace |
| 349 |
@itemx -W |
| 350 |
@opindex --whitespace |
| 351 |
@opindex -W |
| 352 |
Use whitespace (one or more spaces and/or tabs) for field delimiters. |
| 353 |
Leading whitespace is ignored, trailing whitespace results in an empty field. |
| 354 |
TAB character will be used as output field separator. |
| 355 |
If @option{--output-delimiter} is also used, it will override the output |
| 356 |
field delimiter. |
| 357 |
|
| 358 |
@item --seed |
| 359 |
@itemx -S |
| 360 |
@opindex --seed |
| 361 |
@opindex -S |
| 362 |
Select a specific random seed. By default, GNU Datamash uses getrandom(2), |
| 363 |
which should be suitable for most purposes. You may wish to force a specific |
| 364 |
seed if you either wish to draw on a specific entropy source or for ensuring |
| 365 |
the reproducibility of a specific test. |
| 366 |
|
| 367 |
@item --zero-terminated |
| 368 |
@itemx -z |
| 369 |
@opindex --zero-terminated |
| 370 |
@opindex -z |
| 371 |
End lines with a 0 byte, not newline. |
| 372 |
|
| 373 |
@item --help |
| 374 |
@itemx -h |
| 375 |
@opindex --help |
| 376 |
@opindex -h |
| 377 |
Print an informative help message on standard output and exit |
| 378 |
successfully. |
| 379 |
|
| 380 |
@item --version |
| 381 |
@itemx -V |
| 382 |
@opindex --version |
| 383 |
@opindex -V |
| 384 |
Print the version number and licensing information of Datamash on |
| 385 |
standard output and then exit successfully. |
| 386 |
|
| 387 |
@end table |
| 388 |
|
| 389 |
@node Available Operations |
| 390 |
@chapter Available operations in @command{datamash} |
| 391 |
|
| 392 |
@table @asis |
| 393 |
@item Primary operations: |
| 394 |
@cindex primary operations |
| 395 |
@cindex operations, primary |
| 396 |
|
| 397 |
@table @option |
| 398 |
@item groupby |
| 399 |
alternative syntax for @option{--group} |
| 400 |
@item crosstab |
| 401 |
cross-tabulate two fields (also known as 'pivot-tables') |
| 402 |
@item transpose |
| 403 |
transpose rows, columns of a text file |
| 404 |
@item reverse |
| 405 |
reverse fields in each line of a text file |
| 406 |
@item check |
| 407 |
verify tabular structure of input (ensure same number of fields in all lines) |
| 408 |
@end table |
| 409 |
|
| 410 |
@item Line-Filtering operation: |
| 411 |
@cindex line filtering operation |
| 412 |
@cindex operations, line filtering |
| 413 |
|
| 414 |
@table @option |
| 415 |
@item rmdup |
| 416 |
remove lines with duplicated key value |
| 417 |
@end table |
| 418 |
|
| 419 |
@item Per-Line operations: |
| 420 |
@cindex Per-Line operations |
| 421 |
@cindex operations, per-line |
| 422 |
|
| 423 |
@table @option |
| 424 |
@item base64 |
| 425 |
encode the field as base64 |
| 426 |
@item debase64 |
| 427 |
decode the field as base64. Exit with an error if the field is invalid base64 |
| 428 |
value which cannot be decoded. |
| 429 |
@item md5 |
| 430 |
calculates md5 hash of the field |
| 431 |
@item sha1 |
| 432 |
calculates sha1 hash of the field |
| 433 |
@item sha224 |
| 434 |
calculates sha224 hash of the field |
| 435 |
@item sha256 |
| 436 |
calculates sha256 hash of the field |
| 437 |
@item sha384 |
| 438 |
calculates sha384 hash of the field |
| 439 |
@item sha512 |
| 440 |
calculates sha512 hash of the field |
| 441 |
@item dirname |
| 442 |
extracts the directory name of the field (assuming the field is a file name). |
| 443 |
Similar to @command{dirname(1)}. |
| 444 |
@item basename |
| 445 |
extracts the base file name of the field (assuming the field is a file name). |
| 446 |
Similar to @command{basename(1)}. |
| 447 |
@item extname |
| 448 |
extracts the extension of the file name of the field (assuming the field is a |
| 449 |
file name). |
| 450 |
@item extname |
| 451 |
extracts the base file name of the field without the extension (assuming the |
| 452 |
field is a file name). |
| 453 |
@item getnum |
| 454 |
extract a number from the field. @code{getnum} accepts an optional single |
| 455 |
letter option @samp{n/i/d/p/h/o} affecting the detected value. |
| 456 |
@item cut |
| 457 |
copy input field to output field (similar to @command{cut(1)}). |
| 458 |
When the @code{cut} operation is given a list of fields, the fields are copied |
| 459 |
in the given order (in contrast to @command{cut(1)}). |
| 460 |
@item echo |
| 461 |
an alias for @code{cut}. |
| 462 |
@end table |
| 463 |
|
| 464 |
@item Group-by Numeric operations: |
| 465 |
@cindex numeric operations |
| 466 |
@cindex operations, numeric |
| 467 |
|
| 468 |
@table @option |
| 469 |
@item sum |
| 470 |
sum the of values |
| 471 |
@item min |
| 472 |
minimum value |
| 473 |
@item max |
| 474 |
maximum value |
| 475 |
@item absmin |
| 476 |
minimum of the absolute values |
| 477 |
@item absmax |
| 478 |
maximum of the absolute values |
| 479 |
@item range |
| 480 |
range of values (maximum - minimum) |
| 481 |
@end table |
| 482 |
|
| 483 |
@item Group-By Textual/Numeric operations: |
| 484 |
@cindex Textual operations |
| 485 |
@cindex operations, textual |
| 486 |
|
| 487 |
@table @option |
| 488 |
@item count |
| 489 |
count number of elements in the group |
| 490 |
@item first |
| 491 |
the first value of the group |
| 492 |
@item last |
| 493 |
the last value of the group |
| 494 |
@item rand |
| 495 |
one random value from the group |
| 496 |
@item unique |
| 497 |
comma-separated sorted list of unique values |
| 498 |
@item uniq |
| 499 |
an alias for @code{unique}. |
| 500 |
|
| 501 |
@option{--collapse-delimiter} can be used to use a different character |
| 502 |
than comma. |
| 503 |
|
| 504 |
@item collapse |
| 505 |
comma-separated list of all input values |
| 506 |
|
| 507 |
@option{--collapse-delimiter} can be used to use a different character |
| 508 |
than comma. |
| 509 |
|
| 510 |
@item countunique |
| 511 |
number of unique/distinct values |
| 512 |
@end table |
| 513 |
|
| 514 |
@item Group-By Statistical operations: |
| 515 |
@cindex Statistical operations |
| 516 |
@cindex operations, statistical |
| 517 |
|
| 518 |
@table @option |
| 519 |
@item mean |
| 520 |
mean of the values |
| 521 |
@item geomean |
| 522 |
geometric mean of the values |
| 523 |
@item harmmean |
| 524 |
harmonic mean of the values |
| 525 |
@item trimmean |
| 526 |
trimmed mean of the values |
| 527 |
@item ms |
| 528 |
mean square of the values |
| 529 |
@item rms |
| 530 |
root mean square of the values |
| 531 |
@item median |
| 532 |
median value |
| 533 |
@item q1 |
| 534 |
1st quartile value |
| 535 |
@item q3 |
| 536 |
3rd quartile value |
| 537 |
@item iqr |
| 538 |
inter-quartile range |
| 539 |
@item perc |
| 540 |
percentile value |
| 541 |
@item mode |
| 542 |
mode value (most common value) |
| 543 |
@item antimode |
| 544 |
anti-mode value (least common value) |
| 545 |
@item pstdev |
| 546 |
population standard deviation |
| 547 |
@item sstdev |
| 548 |
sample standard deviation |
| 549 |
@item pvar |
| 550 |
population variance |
| 551 |
@item svar |
| 552 |
sample variance |
| 553 |
@item mad |
| 554 |
Median Absolute Deviation, |
| 555 |
scaled by a constant 1.4826 for normal distributions |
| 556 |
@item madraw |
| 557 |
Median Absolute Deviation, unscaled |
| 558 |
@item sskew |
| 559 |
skewness of the (sample) group |
| 560 |
@item pskew |
| 561 |
skewness of the (population) group |
| 562 |
@item skurt |
| 563 |
Excess Kurtosis of the (sample) group |
| 564 |
@item pkurt |
| 565 |
Excess Kurtosis of the (population) group |
| 566 |
@item jarque |
| 567 |
p-value of the Jarque-Beta test for normality |
| 568 |
@item dpo |
| 569 |
p-value of the D'Agostino-Pearson Omnibus test for normality. |
| 570 |
@end table |
| 571 |
|
| 572 |
@end table |
| 573 |
|
| 574 |
|
| 575 |
@node Statistical Operations |
| 576 |
@chapter Statistical Operations |
| 577 |
|
| 578 |
@cindex statistics |
| 579 |
@cindex operations, statistical |
| 580 |
@cindex statistical operations |
| 581 |
|
| 582 |
@unnumberedsec Equivalent R functions |
| 583 |
GNU Datamash is designed to closely follow R project's |
| 584 |
(@url{https://www.r-project.org/}) statistical functions. |
| 585 |
See the @file{files/operators.R} file |
| 586 |
for the R equivalent code for each of datamash's operators. |
| 587 |
When building @command{datamash} from source code on your local computer, |
| 588 |
operators are compared to known results of the equivalent R functions. |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
@node Usage Examples |
| 593 |
@chapter Usage Examples |
| 594 |
@cindex usage examples |
| 595 |
@cindex examples, usage |
| 596 |
|
| 597 |
@menu |
| 598 |
* Summary Statistics:: count,min,max,mean,stdev,median,quartiles |
| 599 |
* Header Lines and Column Names:: Using files with header lines |
| 600 |
* Field Delimiters:: Tabs, Whitespace, other delimiters |
| 601 |
* Column Ranges:: Operating on multiple columns |
| 602 |
* Reverse and Transpose:: swapping and transposing rows, columns |
| 603 |
* Groupby on @file{/etc/passwd}:: Groupby, count, collapse |
| 604 |
* Check:: Validate tabular structure |
| 605 |
* Crosstab:: Cross-tabulation (pivot-tables) |
| 606 |
* Rounding numbers:: round, ceil, floor, trunc, frac |
| 607 |
* Binning numbers:: assigning numbers into fixed number of buckets |
| 608 |
* Binning strings:: assigning strings into fixed number of buckets |
| 609 |
* Extracting numeric values:: using getnum |
| 610 |
@end menu |
| 611 |
|
| 612 |
|
| 613 |
@node Summary Statistics |
| 614 |
@section Summary Statistics |
| 615 |
@cindex summary statistics example |
| 616 |
@cindex examples, summary statistics |
| 617 |
|
| 618 |
The following are examples of using @command{datamash} to quickly |
| 619 |
calculate summary statistics. The examples will use a file with three |
| 620 |
fields (name, subject, score) representing grades of students: |
| 621 |
|
| 622 |
@example |
| 623 |
$ cat scores.txt |
| 624 |
Shawn Arts 65 |
| 625 |
Marques Arts 58 |
| 626 |
Fernando Arts 78 |
| 627 |
Paul Arts 63 |
| 628 |
Walter Arts 75 |
| 629 |
... |
| 630 |
@end example |
| 631 |
|
| 632 |
Counting how many students study each subject (@emph{subject} is the |
| 633 |
second field in the input file, thus @option{groupby 2}): |
| 634 |
|
| 635 |
@example |
| 636 |
$ datamash --sort groupby 2 @option{count} 2 < scores.txt |
| 637 |
Arts 19 |
| 638 |
Business 11 |
| 639 |
Engineering 13 |
| 640 |
Health-Medicine 13 |
| 641 |
Life-Sciences 12 |
| 642 |
Social-Sciences 15 |
| 643 |
@end example |
| 644 |
|
| 645 |
@cindex min, examples |
| 646 |
@cindex max, examples |
| 647 |
@cindex examples, min |
| 648 |
@cindex examples, max |
| 649 |
Similarly, find the minimum and maximum score in each subject: |
| 650 |
|
| 651 |
@example |
| 652 |
$ datamash --sort groupby 2 @option{min} 3 @option{max} 3 < scores.txt |
| 653 |
Arts 46 88 |
| 654 |
Business 79 94 |
| 655 |
Engineering 39 99 |
| 656 |
Health-Medicine 72 100 |
| 657 |
Life-Sciences 14 91 |
| 658 |
Social-Sciences 27 90 |
| 659 |
@end example |
| 660 |
|
| 661 |
@cindex mean, examples |
| 662 |
@cindex standard deviation, examples |
| 663 |
@cindex examples, mean |
| 664 |
@cindex examples, standard deviation |
| 665 |
find the mean and (population) standard deviation in each subject: |
| 666 |
|
| 667 |
@example |
| 668 |
$ datamash --sort groupby 2 @option{mean} 3 @option{pstdev} 3 < scores.txt |
| 669 |
Arts 68.947 10.143 |
| 670 |
Business 87.363 4.940 |
| 671 |
Engineering 66.538 19.101 |
| 672 |
Health-Medicine 90.615 8.862 |
| 673 |
Life-Sciences 55.333 19.728 |
| 674 |
Social-Sciences 60.266 16.643 |
| 675 |
@end example |
| 676 |
|
| 677 |
|
| 678 |
@cindex median, examples |
| 679 |
@cindex examples, median |
| 680 |
@cindex quartiles, examples |
| 681 |
@cindex examples, quartiles |
| 682 |
Find the median, first, third quartiles and the inter-quartile range in |
| 683 |
each subject: |
| 684 |
|
| 685 |
@example |
| 686 |
$ datamash --sort groupby 2 @option{median} 3 @option{q1} 3 @option{q3} @ |
| 687 |
3 @option{iqr} 3 < scores.txt |
| 688 |
Arts 71 61.5 75.5 14 |
| 689 |
Business 87 83 92 9 |
| 690 |
Engineering 56 51 83 32 |
| 691 |
Health-Medicine 91 84 100 16 |
| 692 |
Life-Sciences 58.5 44.25 67.75 23.5 |
| 693 |
Social-Sciences 62 55 70.5 15.5 |
| 694 |
@end example |
| 695 |
|
| 696 |
|
| 697 |
@xref{Header Lines and Column Names} for examples of dealing with |
| 698 |
header lines. |
| 699 |
|
| 700 |
@node Header Lines and Column Names |
| 701 |
@section Header Lines and Column Names |
| 702 |
|
| 703 |
@opindex --header-out |
| 704 |
@cindex examples, header |
| 705 |
@cindex examples, header-out |
| 706 |
@cindex header, examples |
| 707 |
@cindex header-out, examples |
| 708 |
@unnumberedsubsec Output Header Lines |
| 709 |
|
| 710 |
If the input does @emph{not} have a header line, use |
| 711 |
@option{--header-out} to add a header in the first line of the output, |
| 712 |
indicating which operation was performed: |
| 713 |
|
| 714 |
@example |
| 715 |
$ datamash --sort @option{--header-out} groupby 2 @option{min} @ |
| 716 |
3 @option{max} 3 < scores.txt |
| 717 |
GroupBy(field-2) min(field-3) max(field-3) |
| 718 |
Arts 46 88 |
| 719 |
Business 79 94 |
| 720 |
Engineering 39 99 |
| 721 |
Health-Medicine 72 100 |
| 722 |
Life-Sciences 14 91 |
| 723 |
Social-Sciences 27 90 |
| 724 |
@end example |
| 725 |
|
| 726 |
|
| 727 |
@unnumberedsubsec Skipping Input Header Lines |
| 728 |
|
| 729 |
@opindex --header-in |
| 730 |
@cindex examples, header |
| 731 |
@cindex examples, header-in |
| 732 |
@cindex header-in, examples |
| 733 |
|
| 734 |
If the input has a header line (first line containing column names), |
| 735 |
use @option{--header-in} to skip the line: |
| 736 |
|
| 737 |
@example |
| 738 |
$ cat scores_h.txt |
| 739 |
Name Major Score |
| 740 |
Shawn Arts 65 |
| 741 |
Marques Arts 58 |
| 742 |
Fernando Arts 78 |
| 743 |
Paul Arts 63 |
| 744 |
... |
| 745 |
|
| 746 |
|
| 747 |
$ datamash --sort @option{--header-in} groupby 2 mean 3 < scores_h.txt |
| 748 |
Arts 68.947 |
| 749 |
Business 87.363 |
| 750 |
Engineering 66.538 |
| 751 |
Health-Medicine 90.615 |
| 752 |
Life-Sciences 55.333 |
| 753 |
Social-Sciences 60.266 |
| 754 |
@end example |
| 755 |
|
| 756 |
If the header line is not skipped, @command{datamash} will show an error |
| 757 |
(due to strict input validation): |
| 758 |
|
| 759 |
@example |
| 760 |
$ datamash groupby 2 mean 3 < scores_h.txt |
| 761 |
datamash: invalid numeric value in line 1 field 3: 'Score' |
| 762 |
@end example |
| 763 |
|
| 764 |
|
| 765 |
@unnumberedsubsec Using Header Lines |
| 766 |
|
| 767 |
@opindex --headers |
| 768 |
@opindex -H |
| 769 |
@cindex examples, headers |
| 770 |
@cindex headers, examples |
| 771 |
|
| 772 |
Column names in the input header lines can be printed |
| 773 |
in the output header lines by using @option{--headers} |
| 774 |
(or @option{-H}, both are equivalent to @option{--header-in --header-out}): |
| 775 |
|
| 776 |
@example |
| 777 |
$ datamash --sort @option{--headers} groupby 2 mean 3 < scores_h.txt |
| 778 |
GroupBy(Major) mean(Score) |
| 779 |
Arts 68.947 |
| 780 |
Business 87.363 |
| 781 |
Engineering 66.538 |
| 782 |
Health-Medicine 90.615 |
| 783 |
Life-Sciences 55.333 |
| 784 |
Social-Sciences 60.266 |
| 785 |
@end example |
| 786 |
|
| 787 |
Or in short form (@option{-sH} instead of @option{--sort --headers}), |
| 788 |
equivalent to the above command: |
| 789 |
|
| 790 |
@example |
| 791 |
$ datamash @option{-sH} groupby 2 mean 3 |
| 792 |
@end example |
| 793 |
|
| 794 |
|
| 795 |
@unnumberedsubsec Column Names |
| 796 |
@cindex column names |
| 797 |
@cindex field names |
| 798 |
|
| 799 |
When the input file has a header line, column names can be used |
| 800 |
instead of column numbers. In the example below, @var{Major} |
| 801 |
is used instead of the value 2, and @var{Score} is used |
| 802 |
instead of the value 3: |
| 803 |
|
| 804 |
@example |
| 805 |
$ datamash --sort --headers groupby Major mean Score < scores_h.txt |
| 806 |
GroupBy(Major) mean(Score) |
| 807 |
Arts 68.947 |
| 808 |
Business 87.363 |
| 809 |
Engineering 66.538 |
| 810 |
Health-Medicine 90.615 |
| 811 |
Life-Sciences 55.333 |
| 812 |
Social-Sciences 60.266 |
| 813 |
@end example |
| 814 |
|
| 815 |
@command{datamash} will read the first line of the input, and deduce |
| 816 |
the correct column number based on the given name. If the column name |
| 817 |
is not found, an error will be printed: |
| 818 |
|
| 819 |
@example |
| 820 |
$ datamash --sort --headers groupby 2 mean @option{Foo} < scores_h.txt |
| 821 |
datamash: column name 'Foo' not found in input file |
| 822 |
@end example |
| 823 |
|
| 824 |
|
| 825 |
Field names must be escaped with a backslash if they start with a digit |
| 826 |
or contain special characters (dash/minus, colons, commas). |
| 827 |
Note the interplay between escaping with backslash and shell quoting. |
| 828 |
The following equivalent command sum the values of a field named @samp{FOO-BAR}: |
| 829 |
|
| 830 |
@example |
| 831 |
$ datamash -H sum FOO\\-BAR < input.txt |
| 832 |
$ datamash -H sum 'FOO\-BAR' < input.txt |
| 833 |
$ datamash -H sum "FOO\\-BAR" < input.txt |
| 834 |
@end example |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
@node Field Delimiters |
| 839 |
@section Field Delimiters |
| 840 |
@cindex field delimiters |
| 841 |
@cindex whitespace delimiters |
| 842 |
@cindex delimiters, whitespace |
| 843 |
@cindex tab delimiters |
| 844 |
@cindex delimiters, tabs |
| 845 |
|
| 846 |
@command{datamash} uses tabs (ASCII character 0x09) as default field |
| 847 |
delimiters. Use @option{-W} to treat one or more consecutive |
| 848 |
whitespace characters as field delimiters. Use @option{-t}, |
| 849 |
@option{--field-separator} to set a custom field delimiter. |
| 850 |
|
| 851 |
The following examples illustrate the various options. |
| 852 |
|
| 853 |
By default, fields are separated by a single tab. Multiple tabs |
| 854 |
denotes multiple fields (this is consistent with GNU coreutils' |
| 855 |
@command{cut}): |
| 856 |
|
| 857 |
@example |
| 858 |
$ printf '1\t\t2\n' | datamash sum 3 |
| 859 |
2 |
| 860 |
$ printf '1\t\t2\n' | cut -f3 |
| 861 |
2 |
| 862 |
@end example |
| 863 |
|
| 864 |
Every tab separates two fields. A line starting with a tab thus starts |
| 865 |
with an empty field, and a line ending with a tab ends with an empty field. |
| 866 |
|
| 867 |
Using @option{-W}, one or more consecutive whitespace characters |
| 868 |
are treated as a single field delimiter: |
| 869 |
|
| 870 |
@example |
| 871 |
$ printf '1 \t 2\n' | datamash -W sum 2 |
| 872 |
2 |
| 873 |
$ printf '1 \t 2\n' | datamash -W sum 3 |
| 874 |
datamash: invalid input: field 3 requested, line 1 has only 2 fields |
| 875 |
@end example |
| 876 |
|
| 877 |
With @option{-W}, leading whitespace is ignored, but trailing whitespace |
| 878 |
is significant. A line starting with one or more consecutive whitespace |
| 879 |
characters followed by a non-whitespace character starts with a non-empty |
| 880 |
field. A line ending with one or more consecutive whitespace characters |
| 881 |
ends with an empty field. |
| 882 |
|
| 883 |
Using @option{-t}, a custom field delimiter character can be specified. |
| 884 |
Multiple consecutive delimiters are treated as multiple fields: |
| 885 |
|
| 886 |
@example |
| 887 |
$ printf '1,10,,100\n' | datamash -t, sum 4 |
| 888 |
100 |
| 889 |
@end example |
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
@node Column Ranges |
| 894 |
@section Column Ranges |
| 895 |
@cindex column ranges |
| 896 |
@cindex ranges, columns |
| 897 |
@cindex multiple columns |
| 898 |
|
| 899 |
@command{datamash} accepts column ranges such as @var{1,2,3} and @var{1-3}. |
| 900 |
|
| 901 |
|
| 902 |
Simulating input with multiple columns: |
| 903 |
|
| 904 |
@example |
| 905 |
$ seq 100 | paste - - - - |
| 906 |
1 2 3 4 |
| 907 |
5 6 7 8 |
| 908 |
9 10 11 12 |
| 909 |
13 14 15 16 |
| 910 |
17 18 19 20 |
| 911 |
... |
| 912 |
@end example |
| 913 |
|
| 914 |
The following are equivalent: |
| 915 |
|
| 916 |
@example |
| 917 |
$ seq 100 | paste - - - - | datamash sum @option{1 sum 2 sum 3 sum 4} |
| 918 |
1225 1250 1275 1300 |
| 919 |
|
| 920 |
$ seq 100 | paste - - - - | datamash sum @option{1,2,3,4} |
| 921 |
1225 1250 1275 1300 |
| 922 |
|
| 923 |
$ seq 100 | paste - - - - | datamash sum @option{1-4} |
| 924 |
1225 1250 1275 1300 |
| 925 |
|
| 926 |
$ seq 100 | paste - - - - | datamash sum @option{1-3,4} |
| 927 |
1225 1250 1275 1300 |
| 928 |
@end example |
| 929 |
|
| 930 |
Ranges can be used with multiple operations: |
| 931 |
|
| 932 |
@example |
| 933 |
$ seq 100 | paste - - - - | datamash @option{sum 1-4 mean 1-4} |
| 934 |
1225 1250 1275 1300 49 50 51 52 |
| 935 |
@end example |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
@node Reverse and Transpose |
| 941 |
@section Reverse and Transpose |
| 942 |
|
| 943 |
@unnumberedsubsec Transpose |
| 944 |
@cindex transpose |
| 945 |
@cindex swap rows, columns |
| 946 |
|
| 947 |
Use @option{transpose} to swap rows and columns in a file: |
| 948 |
|
| 949 |
@example |
| 950 |
$ cat input.txt |
| 951 |
Sample Year Count |
| 952 |
A 2014 1002 |
| 953 |
B 2013 990 |
| 954 |
C 2014 2030 |
| 955 |
D 2014 599 |
| 956 |
|
| 957 |
$ datamash @option{transpose} < input.txt |
| 958 |
Sample A B C D |
| 959 |
Year 2014 2013 2014 2014 |
| 960 |
Count 1002 990 2030 599 |
| 961 |
@end example |
| 962 |
|
| 963 |
|
| 964 |
@cindex strict mode |
| 965 |
@cindex input validation, transpose |
| 966 |
@cindex transpose, input validation |
| 967 |
By default, @option{transpose} verifies the input has the same number |
| 968 |
of fields in each line, and fails with an error otherwise: |
| 969 |
|
| 970 |
@example |
| 971 |
$ cat input.txt |
| 972 |
Sample Year Count |
| 973 |
A 2014 1002 |
| 974 |
B 2013 |
| 975 |
C 2014 2030 |
| 976 |
D 2014 599 |
| 977 |
|
| 978 |
|
| 979 |
$ datamash @option{transpose} < input1.txt |
| 980 |
datamash: transpose input error: line 3 has 2 fields (previous lines had 3); |
| 981 |
see --help to disable strict mode |
| 982 |
@end example |
| 983 |
|
| 984 |
Use @option{--no-strict} to allow missing values: |
| 985 |
|
| 986 |
@opindex --no-strict |
| 987 |
@cindex strict, transpose |
| 988 |
@cindex transpose, strict |
| 989 |
@example |
| 990 |
$ datamash @option{--no-strict} transpose < input1.txt |
| 991 |
Sample A B C D |
| 992 |
Year 2014 2013 2014 2014 |
| 993 |
Count 1002 N/A 2030 599 |
| 994 |
@end example |
| 995 |
|
| 996 |
@opindex --filler |
| 997 |
@cindex missing values, transpose |
| 998 |
@cindex transpose, missing values |
| 999 |
@cindex transpose, filler value |
| 1000 |
Use @option{--filler} to set the missing-field filler value: |
| 1001 |
|
| 1002 |
@example |
| 1003 |
$ datamash --no-strict @option{--filler XYZ} transpose < input1.txt |
| 1004 |
Sample A B C D |
| 1005 |
Year 2014 2013 2014 2014 |
| 1006 |
Count 1002 XYZ 2030 599 |
| 1007 |
@end example |
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
@unnumberedsubsec Reverse |
| 1012 |
@cindex reverse columns |
| 1013 |
@cindex columns, reverse |
| 1014 |
|
| 1015 |
Use @option{reverse} to reverse the fields order in a file: |
| 1016 |
|
| 1017 |
@example |
| 1018 |
$ cat input.txt |
| 1019 |
Sample Year Count |
| 1020 |
A 2014 1002 |
| 1021 |
B 2013 990 |
| 1022 |
C 2014 2030 |
| 1023 |
D 2014 599 |
| 1024 |
|
| 1025 |
$ datamash @option{reverse} < input.txt |
| 1026 |
Count Year Sample |
| 1027 |
1002 2014 A |
| 1028 |
990 2013 B |
| 1029 |
2030 2014 C |
| 1030 |
599 2014 D |
| 1031 |
@end example |
| 1032 |
|
| 1033 |
@cindex reverse, strict |
| 1034 |
@cindex strict, reverse |
| 1035 |
By default, reverse verifies the input has the same number of fields |
| 1036 |
in each line, and fails with an error otherwise. Use |
| 1037 |
@option{--no-strict} to disable this behavior (see section |
| 1038 |
above for an example). |
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
@unnumberedsubsec Combining Reverse and Transpose |
| 1043 |
|
| 1044 |
@cindex tac |
| 1045 |
@cindex reversing lines |
| 1046 |
@cindex reverse, and transpose |
| 1047 |
@cindex transpose, and reverse |
| 1048 |
Reverse and Transpose can be combined to achieve various manipulations. |
| 1049 |
(reminder: @url{https://www.gnu.org/software/coreutils/tac,tac} can be |
| 1050 |
used to reverse lines in a file): |
| 1051 |
|
| 1052 |
@example |
| 1053 |
$ cat input.txt |
| 1054 |
A 1 xx |
| 1055 |
B 2 yy |
| 1056 |
C 3 zz |
| 1057 |
|
| 1058 |
|
| 1059 |
$ tac input.txt |
| 1060 |
C 3 zz |
| 1061 |
B 2 yy |
| 1062 |
A 1 xx |
| 1063 |
|
| 1064 |
|
| 1065 |
$ tac input.txt | datamash reverse |
| 1066 |
zz 3 C |
| 1067 |
yy 2 B |
| 1068 |
xx 1 A |
| 1069 |
|
| 1070 |
|
| 1071 |
$ cat input.txt | datamash reverse | datamash transpose |
| 1072 |
xx yy zz |
| 1073 |
1 2 3 |
| 1074 |
A B C |
| 1075 |
|
| 1076 |
$ tac input.txt | datamash reverse | datamash transpose |
| 1077 |
zz yy xx |
| 1078 |
3 2 1 |
| 1079 |
C B A |
| 1080 |
@end example |
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
@node Groupby on @file{/etc/passwd} |
| 1085 |
@section Groupby on @file{/etc/passwd} |
| 1086 |
@cindex groupby |
| 1087 |
@cindex @file{/etc/passwd}, examples |
| 1088 |
@cindex examples, @file{/etc/passwd} |
| 1089 |
|
| 1090 |
@command{datamash} with the @option{groupby} operation mode |
| 1091 |
can be used to aggregate information. |
| 1092 |
|
| 1093 |
Using this simulated @file{/etc/passwd} file as input: |
| 1094 |
|
| 1095 |
@example |
| 1096 |
$ cat passwd |
| 1097 |
root:x:0:0:root:/root:/bin/bash |
| 1098 |
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin |
| 1099 |
bin:x:2:2:bin:/bin:/usr/sbin/nologin |
| 1100 |
sys:x:3:3:sys:/dev:/usr/sbin/nologin |
| 1101 |
sync:x:4:65534:sync:/bin:/bin/sync |
| 1102 |
games:x:5:60:games:/usr/games:/usr/sbin/nologin |
| 1103 |
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin |
| 1104 |
lp:x:7:7:lp:/var/spool/lpd:/usr/sbin/nologin |
| 1105 |
mail:x:8:8:mail:/var/mail:/usr/sbin/nologin |
| 1106 |
news:x:9:9:news:/var/spool/news:/usr/sbin/nologin |
| 1107 |
uucp:x:10:10:uucp:/var/spool/uucp:/usr/sbin/nologin |
| 1108 |
proxy:x:13:13:proxy:/bin:/usr/sbin/nologin |
| 1109 |
www-data:x:33:33:www-data:/var/www:/usr/sbin/nologin |
| 1110 |
backup:x:34:34:backup:/var/backups:/usr/sbin/nologin |
| 1111 |
list:x:38:38:Mailing List Manager:/var/list:/usr/sbin/nologin |
| 1112 |
mysql:x:115:124:MySQL Server,,,:/var/lib/mysql:/bin/false |
| 1113 |
sshd:x:116:65534::/var/run/sshd:/usr/sbin/nologin |
| 1114 |
guest:x:118:125:Guest,,,:/tmp/guest-home.phc17z:/bin/bash |
| 1115 |
gordon:x:1004:1000:Assaf Gordon,,,,:/home/gordon:/bin/bash |
| 1116 |
charles:x:1005:1000:Charles,,,,:/home/charles:/bin/bash |
| 1117 |
alice:x:1006:1000:Alice,,,,:/home/alice:/bin/bash |
| 1118 |
bob:x:1007:1000:Bob,,,,:/home/bob:/bin/bash |
| 1119 |
postgres:x:119:126:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash |
| 1120 |
rabbitmq:x:125:138:RabbitMQ messaging server,,,:/var/lib/rabbitmq:/bin/false |
| 1121 |
redis:x:126:140:redis server,,,:/var/lib/redis:/bin/false |
| 1122 |
postfix:x:127:141::/var/spool/postfix:/bin/false |
| 1123 |
@end example |
| 1124 |
|
| 1125 |
@opindex -t |
| 1126 |
@opindex --field-separator |
| 1127 |
Parameter @option{-t} is used to indicate the field separator @var{:} |
| 1128 |
(instead of the default @var{tab}). |
| 1129 |
|
| 1130 |
@cindex groupby, and count |
| 1131 |
@cindex count |
| 1132 |
@cindex login shell, examples |
| 1133 |
Aggregate (@option{groupby}) login shells (column 7) and |
| 1134 |
@option{count} how many users use each: |
| 1135 |
|
| 1136 |
@example |
| 1137 |
$ datamash -t: --sort groupby 7 count 7 < passwd |
| 1138 |
/bin/bash:7 |
| 1139 |
/bin/false:4 |
| 1140 |
/bin/sync:1 |
| 1141 |
/usr/sbin/nologin:14 |
| 1142 |
@end example |
| 1143 |
|
| 1144 |
@cindex groupby, and collapse |
| 1145 |
@cindex collapse |
| 1146 |
Aggregate (@option{groupby}) login shells (column 7) and print |
| 1147 |
comma-separated list of users (column 1) for each shell |
| 1148 |
(@option{collapse}): |
| 1149 |
|
| 1150 |
@example |
| 1151 |
$ cat passwd | datamash -t: --sort groupby 7 collapse 1 |
| 1152 |
/bin/bash:root,guest,gordon,charles,alice,bob,postgres |
| 1153 |
/bin/false:mysql,rabbitmq,redis,postfix |
| 1154 |
/bin/sync:sync |
| 1155 |
/usr/sbin/nologin:daemon,bin,sys,games,man,lp,mail,news,uucp,proxy@ |
| 1156 |
,www-data,backup,list,sshd |
| 1157 |
@end example |
| 1158 |
|
| 1159 |
Aggregate unix-groups (column 4) and print |
| 1160 |
comma-separated list of users (column 1) for in each group: |
| 1161 |
|
| 1162 |
@example |
| 1163 |
$ datamash -t: --sort groupby 4 collapse 1 < /etc/passwd |
| 1164 |
0:root |
| 1165 |
1:daemon |
| 1166 |
10:uucp |
| 1167 |
1000:gordon,charles,alice,bob |
| 1168 |
12:man |
| 1169 |
124:mysql |
| 1170 |
125:guest |
| 1171 |
126:postgres |
| 1172 |
13:proxy |
| 1173 |
138:rabbitmq |
| 1174 |
140:redis |
| 1175 |
141:postfix |
| 1176 |
2:bin |
| 1177 |
3:sys |
| 1178 |
33:www-data |
| 1179 |
34:backup |
| 1180 |
38:list |
| 1181 |
60:games |
| 1182 |
65534:sync,sshd |
| 1183 |
7:lp |
| 1184 |
8:mail |
| 1185 |
9:news |
| 1186 |
@end example |
| 1187 |
|
| 1188 |
|
| 1189 |
|
| 1190 |
@node Check |
| 1191 |
@section Check - checking tabular structure |
| 1192 |
@cindex check |
| 1193 |
@cindex checking tabular structure |
| 1194 |
|
| 1195 |
@command{datamash} @option{check} validates the tabular structure of a |
| 1196 |
file, ensuring all lines have the same number of |
| 1197 |
fields. @option{check} is meant to be used in scripting and automation |
| 1198 |
pipelines, as it will terminate with non-zero exit code if the file is |
| 1199 |
not well structured, while also printing detailed context information |
| 1200 |
about the offending lines: |
| 1201 |
|
| 1202 |
@example |
| 1203 |
$ cat good.txt |
| 1204 |
A 1 ww |
| 1205 |
B 2 xx |
| 1206 |
C 3 yy |
| 1207 |
D 4 zz |
| 1208 |
|
| 1209 |
|
| 1210 |
$ cat bad.txt |
| 1211 |
A 1 ww |
| 1212 |
B 2 xx |
| 1213 |
C 3 |
| 1214 |
D 4 zz |
| 1215 |
|
| 1216 |
|
| 1217 |
$ datamash check < good.txt && echo ok || echo fail |
| 1218 |
4 lines, 3 fields |
| 1219 |
ok |
| 1220 |
|
| 1221 |
|
| 1222 |
$ datamash check < bad.txt && echo ok || echo fail |
| 1223 |
line 2 (3 fields): |
| 1224 |
B 2 xx |
| 1225 |
line 3 (2 fields): |
| 1226 |
C 3 |
| 1227 |
datamash: check failed: line 3 has 2 fields (previous line had 3) |
| 1228 |
fail |
| 1229 |
@end example |
| 1230 |
|
| 1231 |
@subsection Expected number of lines/fields |
| 1232 |
|
| 1233 |
@option{check} accepts optional @var{lines} and @var{fields} and will |
| 1234 |
return failure if the input does not have the requested number of lines/fields. |
| 1235 |
|
| 1236 |
@exdent The syntax is: |
| 1237 |
|
| 1238 |
@example |
| 1239 |
datamash check [@var{N} lines] [@var{N} fields] |
| 1240 |
@end example |
| 1241 |
|
| 1242 |
@exdent Usage examples: |
| 1243 |
|
| 1244 |
@example |
| 1245 |
$ cat file.txt |
| 1246 |
A 1 ww |
| 1247 |
B 2 xx |
| 1248 |
C 3 yy |
| 1249 |
D 4 zz |
| 1250 |
|
| 1251 |
$ datamash check 4 lines < file.txt && echo ok |
| 1252 |
4 lines, 3 fields |
| 1253 |
ok |
| 1254 |
|
| 1255 |
$ datamash check 3 fields < file.txt && echo ok |
| 1256 |
4 lines, 3 fields |
| 1257 |
ok |
| 1258 |
|
| 1259 |
$ datamash check 4 lines 3 fields < file.txt && echo ok |
| 1260 |
4 lines, 3 fields |
| 1261 |
ok |
| 1262 |
|
| 1263 |
$ datamash check 7 fields < file.txt && echo ok |
| 1264 |
line 1 (3 fields): |
| 1265 |
A 1 ww |
| 1266 |
datamash: check failed: line 1 has 3 fields (expecting 22) |
| 1267 |
|
| 1268 |
$ datamash check 10 lines < file.txt && echo ok |
| 1269 |
datamash: check failed: input had 4 lines (expecting 10) |
| 1270 |
@end example |
| 1271 |
|
| 1272 |
For convenience, @var{line},@var{row},@var{rows} |
| 1273 |
can be used instead of @var{lines}; |
| 1274 |
@var{field},@var{columns},@var{column},@var{col} can be used |
| 1275 |
instead of @var{fields}. |
| 1276 |
The following are all equivalent: |
| 1277 |
|
| 1278 |
@example |
| 1279 |
datamash check 4 lines 10 fields < file.txt |
| 1280 |
datamash check 4 rows 10 columns < file.txt |
| 1281 |
datamash check 10 col 4 row < file.txt |
| 1282 |
@end example |
| 1283 |
|
| 1284 |
|
| 1285 |
@subsection checks in automation scripts |
| 1286 |
|
| 1287 |
@cindex fail fast |
| 1288 |
@cindex shell scripts, check |
| 1289 |
@cindex check, in automation and shell scripts |
| 1290 |
In pipeline/automation context, it is often beneficial to validate |
| 1291 |
files as early as possible (immediately after file is created, as in |
| 1292 |
@url{https://en.wikipedia.org/wiki/Fail-fast, fail-fast methodology}). |
| 1293 |
A typical usage in a shell script would be: |
| 1294 |
|
| 1295 |
@example |
| 1296 |
@verbatim |
| 1297 |
#!/bin/sh |
| 1298 |
|
| 1299 |
die() |
| 1300 |
{ |
| 1301 |
base=$(basename "$0") |
| 1302 |
echo "$base: error: $@" >&2 |
| 1303 |
exit 1 |
| 1304 |
} |
| 1305 |
|
| 1306 |
custom pipeline-or-program > output.txt \ |
| 1307 |
|| die "program failed" |
| 1308 |
|
| 1309 |
datamash check < output.txt \ |
| 1310 |
|| die "'output.txt' has invalid structure (missing fields)" |
| 1311 |
@end verbatim |
| 1312 |
@end example |
| 1313 |
|
| 1314 |
If the generated @file{output.txt} file has invalid structure |
| 1315 |
(i.e. missing fields), @command{datamash} will print the @file{stderr} |
| 1316 |
enough details to help in troubleshooting (line numbers and offending |
| 1317 |
line's content). |
| 1318 |
|
| 1319 |
@node Crosstab |
| 1320 |
@section Crosstab - Cross-Tabulation (pivot-tables) |
| 1321 |
@cindex crosstab |
| 1322 |
@cindex pivot tables |
| 1323 |
@cindex cross tabulation |
| 1324 |
|
| 1325 |
Cross-tabulation compares the relationship between two fields. |
| 1326 |
Given the following input file: |
| 1327 |
|
| 1328 |
@example |
| 1329 |
$ cat input.txt |
| 1330 |
a x 3 |
| 1331 |
a y 7 |
| 1332 |
b x 21 |
| 1333 |
a x 40 |
| 1334 |
@end example |
| 1335 |
|
| 1336 |
@opindex count |
| 1337 |
@cindex count, crosstab and |
| 1338 |
Show cross-tabulation between the first field (a/b) and the second |
| 1339 |
field (x/y) - counting how many times each pair appears (note: sorting |
| 1340 |
is required): |
| 1341 |
|
| 1342 |
@example |
| 1343 |
$ datamash -s crosstab 1,2 < input.txt |
| 1344 |
x y |
| 1345 |
a 2 1 |
| 1346 |
b 1 N/A |
| 1347 |
@end example |
| 1348 |
|
| 1349 |
The default operation is @option{count} - in the above example, |
| 1350 |
@var{a} and @var{x} appear twice in the input file, while @var{b} and @var{y} |
| 1351 |
never appear together. |
| 1352 |
|
| 1353 |
An optional grouping operation can be used instead of counting. |
| 1354 |
|
| 1355 |
@opindex sum |
| 1356 |
@cindex sum, crosstab and |
| 1357 |
@cindex crosstab and sum |
| 1358 |
For each pair, @option{sum} the values in the third column: |
| 1359 |
|
| 1360 |
@example |
| 1361 |
$ datamash -s crosstab 1,2 sum 3 < input.txt |
| 1362 |
x y |
| 1363 |
a 43 7 |
| 1364 |
b 21 N/A |
| 1365 |
@end example |
| 1366 |
|
| 1367 |
@opindex unique |
| 1368 |
@cindex unique, crosstab and |
| 1369 |
@cindex crosstab and unique |
| 1370 |
For each pair, list all @option{unique} values in the third column: |
| 1371 |
|
| 1372 |
@example |
| 1373 |
$ datamash -s crosstab 1,2 unique 3 < input.txt |
| 1374 |
x y |
| 1375 |
a 3,40 7 |
| 1376 |
b 21 N/A |
| 1377 |
@end example |
| 1378 |
|
| 1379 |
@opindex --header-out |
| 1380 |
@cindex --header-out, crosstab and |
| 1381 |
@cindex crosstab and --header-out |
| 1382 |
Note that using @option{--header-out} with crosstab prints a line showing |
| 1383 |
how to interpret the rows and columns, and what operation was used. |
| 1384 |
|
| 1385 |
@example |
| 1386 |
$ datamash -s --header-in --header-out crosstab 1,2 < input.txt |
| 1387 |
GroupBy(a) GroupBy(x) count(a) |
| 1388 |
x y |
| 1389 |
a 1 1 |
| 1390 |
b 1 N/A |
| 1391 |
@end example |
| 1392 |
|
| 1393 |
@node Rounding numbers |
| 1394 |
@section Rounding numbers |
| 1395 |
|
| 1396 |
@cindex rounding numbers |
| 1397 |
@opindex round |
| 1398 |
@opindex ceil |
| 1399 |
@opindex floor |
| 1400 |
@opindex trunc |
| 1401 |
@opindex frac |
| 1402 |
|
| 1403 |
The following demonstrate the different rounding operations: |
| 1404 |
|
| 1405 |
@example |
| 1406 |
$ ( echo X ; seq -1.25 0.25 1.25 ) \ |
| 1407 |
| datamash --full -H round 1 ceil 1 floor 1 trunc 1 frac 1 |
| 1408 |
|
| 1409 |
X round(X) ceil(X) floor(X) trunc(X) frac(X) |
| 1410 |
-1.25 -1 -1 -2 -1 -0.25 |
| 1411 |
-1.00 -1 -1 -1 -1 0 |
| 1412 |
-0.75 -1 0 -1 0 -0.75 |
| 1413 |
-0.50 -1 0 -1 0 -0.5 |
| 1414 |
-0.25 0 0 -1 0 -0.25 |
| 1415 |
0.00 0 0 0 0 0 |
| 1416 |
0.25 0 1 0 0 0.25 |
| 1417 |
0.50 1 1 0 0 0.5 |
| 1418 |
0.75 1 1 0 0 0.75 |
| 1419 |
1.00 1 1 1 1 0 |
| 1420 |
1.25 1 2 1 1 0.25 |
| 1421 |
@end example |
| 1422 |
|
| 1423 |
|
| 1424 |
@node Binning numbers |
| 1425 |
@section Binning numbers |
| 1426 |
@opindex bin |
| 1427 |
@cindex buckets, binning numbers |
| 1428 |
@cindex binning numbers |
| 1429 |
|
| 1430 |
Bin input values into buckets of size 5: |
| 1431 |
|
| 1432 |
@example |
| 1433 |
$ ( echo X ; seq -10 2.5 10 ) \ |
| 1434 |
| datamash -H --full bin:5 1 |
| 1435 |
X bin(X) |
| 1436 |
-10.0 -10 |
| 1437 |
-7.5 -10 |
| 1438 |
-5.0 -5 |
| 1439 |
-2.5 -5 |
| 1440 |
0.0 0 |
| 1441 |
2.5 0 |
| 1442 |
5.0 5 |
| 1443 |
7.5 5 |
| 1444 |
10.0 10 |
| 1445 |
@end example |
| 1446 |
|
| 1447 |
@node Binning strings |
| 1448 |
@section Binning strings |
| 1449 |
@opindex strbin |
| 1450 |
@cindex buckets, binning strings |
| 1451 |
@cindex binning strings |
| 1452 |
|
| 1453 |
Hash any string input value into a numeric integer. |
| 1454 |
A typical usage would be to split an input file |
| 1455 |
into @var{N} chunks, ensuring that all values of a certain key will |
| 1456 |
be stored in the same chunk: |
| 1457 |
|
| 1458 |
@example |
| 1459 |
$ cat input.txt |
| 1460 |
PatientA 10 |
| 1461 |
PatientB 11 |
| 1462 |
PatientC 12 |
| 1463 |
PatientA 14 |
| 1464 |
PatientC 15 |
| 1465 |
@end example |
| 1466 |
|
| 1467 |
Each patient ID is hashed into a bin between 0 and 9 |
| 1468 |
and printed in the last field: |
| 1469 |
|
| 1470 |
@example |
| 1471 |
$ datamash --full strbin 1 < input.txt |
| 1472 |
PatientA 10 5 |
| 1473 |
PatientB 11 6 |
| 1474 |
PatientC 12 7 |
| 1475 |
PatientA 14 5 |
| 1476 |
PatientC 15 7 |
| 1477 |
@end example |
| 1478 |
|
| 1479 |
Splitting the input into chunks can be done with awk: |
| 1480 |
|
| 1481 |
@example |
| 1482 |
@verbatim |
| 1483 |
$ cat input.txt | datamash --full strbin 1 \ |
| 1484 |
| awk '{print > $NF ".txt"}' |
| 1485 |
@end verbatim |
| 1486 |
@end example |
| 1487 |
|
| 1488 |
|
| 1489 |
@node Extracting numeric values |
| 1490 |
@section Extracting numeric values - using getnum |
| 1491 |
@opindex getnum |
| 1492 |
@cindex numbers, extracting from a field |
| 1493 |
|
| 1494 |
The @code{getnum} operation extracts a numeric value from the field: |
| 1495 |
|
| 1496 |
@example |
| 1497 |
@verbatim |
| 1498 |
$ echo zoom-123.45xyz | datamash getnum 1 |
| 1499 |
123.45 |
| 1500 |
@end verbatim |
| 1501 |
@end example |
| 1502 |
|
| 1503 |
|
| 1504 |
@code{getnum} accepts an optional single-letter @var{TYPE} option: |
| 1505 |
|
| 1506 |
@table @option |
| 1507 |
@item getnum:n |
| 1508 |
natural numbers (positive integers, including zero) |
| 1509 |
@item getnum:i |
| 1510 |
integers |
| 1511 |
@item getnum:d |
| 1512 |
decimal point numbers |
| 1513 |
@item getnum:p |
| 1514 |
positive decimal point numbers (this is the default) |
| 1515 |
@item getnum:h |
| 1516 |
hex numbers |
| 1517 |
@item getnum:o |
| 1518 |
octal numbers |
| 1519 |
@end table |
| 1520 |
|
| 1521 |
Examples: |
| 1522 |
|
| 1523 |
@example |
| 1524 |
@verbatim |
| 1525 |
$ echo zoom-123.45xyz | datamash getnum 1 |
| 1526 |
123.45 |
| 1527 |
|
| 1528 |
$ echo zoom-123.45xyz | datamash getnum:n 1 |
| 1529 |
123 |
| 1530 |
|
| 1531 |
$ echo zoom-123.45xyz | datamash getnum:i 1 |
| 1532 |
-123 |
| 1533 |
|
| 1534 |
$ echo zoom-123.45xyz | datamash getnum:d 1 |
| 1535 |
123.45 |
| 1536 |
|
| 1537 |
$ echo zoom-123.45xyz | datamash getnum:p 1 |
| 1538 |
-123.45 |
| 1539 |
|
| 1540 |
# Hex 0x123 = 291 Decimal |
| 1541 |
$ echo zoom-123.45xyz | datamash getnum:h 1 |
| 1542 |
291 |
| 1543 |
|
| 1544 |
# Octal 0123 = 83 Decimal |
| 1545 |
$ echo zoom-123.45xyz | datamash getnum:o 1 |
| 1546 |
83 |
| 1547 |
@end verbatim |
| 1548 |
@end example |
| 1549 |
|
| 1550 |
|
| 1551 |
|
| 1552 |
|
| 1553 |
@node Reporting bugs |
| 1554 |
@chapter Reporting bugs |
| 1555 |
|
| 1556 |
@cindex bug reporting |
| 1557 |
@cindex problems |
| 1558 |
@cindex reporting bugs |
| 1559 |
|
| 1560 |
To report bugs, suggest enhancements or otherwise discuss GNU Datamash, |
| 1561 |
please send electronic mail to @email{bug-datamash@@gnu.org}. |
| 1562 |
|
| 1563 |
@cindex checklist for bug reports |
| 1564 |
For bug reports, please include enough information for the maintainers |
| 1565 |
to reproduce the problem. Generally speaking, that means: |
| 1566 |
|
| 1567 |
@itemize @bullet |
| 1568 |
@item The version numbers of Datamash (which you can find by running |
| 1569 |
@w{@samp{datamash --version}}) and any other program(s) or |
| 1570 |
manual(s) involved. |
| 1571 |
@item Hardware and operating system names and versions. |
| 1572 |
@item The contents of any input files necessary to reproduce the bug. |
| 1573 |
@item The expected behavior and/or output. |
| 1574 |
@item A description of the problem and samples of any erroneous output. |
| 1575 |
@item Options you gave to @command{configure} other than specifying |
| 1576 |
installation directories. |
| 1577 |
@item Anything else that you think would be helpful. |
| 1578 |
@end itemize |
| 1579 |
|
| 1580 |
When in doubt whether something is needed or not, include it. It's |
| 1581 |
better to include too much than to leave out something important. |
| 1582 |
|
| 1583 |
@cindex patches, contributing |
| 1584 |
Patches are welcome; if possible, please make them with @samp{@w{diff |
| 1585 |
-u}} (@pxref{Top,, Overview, diff, Comparing and Merging Files}) and |
| 1586 |
include @file{ChangeLog} entries (@pxref{Change Log,,, emacs, The GNU |
| 1587 |
Emacs Manual}). Please follow the existing coding style. |
| 1588 |
|
| 1589 |
|
| 1590 |
@node GNU Free Documentation License |
| 1591 |
@appendix GNU Free Documentation License |
| 1592 |
|
| 1593 |
@include fdl.texi |
| 1594 |
|
| 1595 |
|
| 1596 |
@node Concept index |
| 1597 |
@unnumbered Concept index |
| 1598 |
|
| 1599 |
@printindex cp |
| 1600 |
|
| 1601 |
@bye |