Debian Codegraph @

man/datamash.x

raw
1 ." GNU Datamash - manual page
2 ." Copyright (C) 2014-2021 Assaf Gordon <[email protected]>
3 ." Copyright (C) 2022-2025 Timothy Rice <[email protected]>
4 [NAME]
5 datamash - command-line calculations
6
7 [>OPTIONS]
8 .PP
9 .SH AVAILABLE OPERATIONS
10 .PP
11
12 .SS "Primary Operations"
13 Primary operations affect the way the file is processed. If used, the
14 primary operation must be listed first. If primary operation is not listed
15 the entire file is processed - either line-by-line (for 'per-line' operations)
16 or all lines as one group (for grouping operations). See Examples section below.
17 .PP
18
19 .TP "\w'\fBcountunique\fR'u+1n"
20 .B groupby X,Y,... op fld ...
21 group the file by given fields. Equivalent to option '\-g'.
22 For each group perform operation \fBop\fR on field \fBfld\fR.
23
24 .TP
25 .B crosstab X,Y [op fld ...]
26 cross-tabulate a file by two fields (cross-tabulation is also known
27 as pivot tables). If no operation is specified, counts how many incidents
28 exist of X,Y.
29
30 .TP
31 .B transpose
32 transpose rows, columns of the input file
33
34 .TP
35 .B reverse
36 reverse field order in each line
37
38 .TP
39 .B check [N lines] [N fields]
40 verify the input file has same number of fields in all lines,
41 or the expected number of lines/fields.
42 number of lines and fields are printed to STDOUT. Exits with non-zero code
43 and prints the offending line if there's a mismatch in the number of lines/
44 fields.
45 .PP
46
47
48 .SS "Line-Filtering operations"
49
50 .TP "\w'\fBcountunique\fR'u+1n"
51 .B rmdup
52 remove lines with duplicated key value
53 .PP
54
55 .SS "Per-Line operations"
56
57 .TP "\w'\fBcountunique\fR'u+1n"
58 .B base64
59 Encode the field as base64
60
61 .TP
62 .B debase64
63 Decode the field as base64, exit with error if invalid base64 string
64
65 .TP
66 .B md5/sha1/sha224/sha256/sha384/sha512
67 Calculate md5/sha1/sha224/sha256/sha384/sha512 hash of the field value
68
69 .TP
70 .B bin[:BUCKET-SIZE]
71 bin numeric values into buckets of size \fBBUCKET-SIZE\fR (defaults to 100).
72
73 .TP
74 .B strbin[:BUCKET-SIZE]
75 hashes the input and returns a numeric integer value between zero and
76 \fBBUCKET-SIZE\fB (defaults to 10).
77
78 .TP
79 .B round/floor/ceil/trunc/frac
80 numeric rounding operations. round (round half away from zero),
81 floor (round down), ceil (ceiling, round up), trunc (truncate, round towards
82 zero), frac (fraction, return fraction part of a decimal-point value).
83
84 .TP
85 .B dirname/basename
86 extract the directory name and the base file name from a given string
87 (same as to dirname(1) and basename(1)).
88
89 .TP
90 .B extname
91 extract the extension of the file name (without the '.').
92
93 .TP
94 .B barename
95 extract the base file name without the extension.
96
97 .TP
98 .B getnum[:TYPE]
99 extract a number from the field. \fBTYPE\fR is optional single letter option
100 n/i/d/p/h/o (see examples below).
101
102 .TP
103 .B cut/echo
104 copy input field to output field (similar to cut(1)).
105 The echo command is simply an alias to cut.
106 .PP
107
108
109 .SS "Numeric Grouping operations"
110
111 .TP "\w'\fBcountunique\fR'u+1n"
112 .B sum
113 sum the of values
114
115 .TP
116 .B min
117 minimum value
118
119 .TP
120 .B max
121 maximum value
122
123 .TP
124 .B absmin
125 minimum of the absolute values
126
127 .TP
128 .B absmax
129 maximum of the absolute values
130
131 .TP
132 .B range
133 the values range (max-min)
134 .PP
135
136 .SS "Textual/Numeric Grouping operations"
137
138 .TP "\w'\fBcountunique\fR'u+1n"
139 .B count
140 count number of elements in the group
141
142 .TP
143 .B first
144 the first value of the group
145
146 .TP
147 .B last
148 the last value of the group
149
150 .TP
151 .B rand
152 one random value from the group
153
154 .TP
155 .B unique/uniq
156 comma-separated sorted list of unique values
157 The uniq command is simply an alias to unique.
158
159 .TP
160 .B collapse
161 comma-separated list of all input values
162
163 .TP
164 .B countunique
165 number of unique/distinct values
166 .PP
167
168
169 .SS "Statistical Grouping operations"
170 A \fBp/s\fR prefix indicates the variant: \fBp\fRopulation or \fBs\fRample.
171 Typically, the \fBs\fRample variant is equivalent with \fBGNU R\fR's
172 internal functions (e.g datamash's \fBsstdev\fR operation is equivalent
173 to R's \fBsd()\fR function).
174 .PP
175
176 .TP "\w'\fBcountunique\fR'u+1n"
177 .B mean
178 mean of the values
179
180 .TP
181 .B geomean
182 geometric mean of the values
183
184 .TP
185 .B harmmean
186 harmonic mean of the values
187
188 .TP
189 .B trimmean[:PERCENT]
190 trimmed mean of the values. \fBPERCENT\fR should be between 0 and 0.5.
191 (\fBtrimmean:0\fR is equivalent to \fBmean\fR. \fBtrimmean:0.5\fR is equivalent
192 to \fBmedian\fR).
193
194 .TP
195 .B ms
196 mean square of the values
197
198 .TP
199 .B rms
200 root mean square of the values
201
202 .TP
203 .B median
204 median value
205
206 .TP
207 .B q1
208 1st quartile value
209
210 .TP
211 .B q3
212 3rd quartile value
213
214 .TP
215 .B iqr
216 inter-quartile range
217
218 .TP
219 .B perc[:PERCENTILE]
220 percentile value \fBPERCENTILE\fR (defaults to 95).
221
222 .TP
223 .B mode
224 mode value (most common value)
225
226 .TP
227 .B antimode
228 anti-mode value (least common value)
229
230 .TP
231 .B pstdev/sstdev
232 population/sample standard deviation
233
234 .TP
235 .B pvar/svar
236 population/sample variance
237
238 .TP
239 .B mad
240 median absolute deviation, scaled by constant 1.4826 for normal distributions
241
242 .TP
243 .B madraw
244 median absolute deviation, unscaled
245
246 .TP
247 .B pskew/sskew
248 skewness of the group
249 values x reported by 'sskew' and 'pskew' operations:
250 .nf
251 x > 0 - positively skewed / skewed right
252 0 > x - negatively skewed / skewed left
253 x > 1 - highly skewed right
254 1 > x > 0.5 - moderately skewed right
255 0.5 > x > \-0.5 - approximately symmetric
256 \-0.5 > x > \-1 - moderately skewed left
257 \-1 > x - highly skewed left
258 .fi
259
260 .TP
261 .B pkurt/skurt
262 excess Kurtosis of the group
263
264 .TP
265 .B jarque/dpo
266 p-value of the Jarque-Beta (\fBjarque\fR) and D'Agostino-Pearson Omnibus
267 (\fBdpo\fR) tests for normality:
268 null hypothesis is normality;
269 low p-Values indicate non-normal data;
270 high p-Values indicate null-hypothesis cannot be rejected.
271
272 .TP
273 .B pcov/scov [X:Y]
274 covariance of fields X and Y
275
276 .TP
277 .B ppearson/spearson [X:Y]
278 Pearson product-moment correlation coefficient [Pearson's R]
279 of fields X and Y
280
281 .TP
282 .B dotprod [X:Y]
283 Scalar product (aka dot product or Euclidean inner product)
284 of fields X and Y
285
286
287 [=EXAMPLES]
288
289 .SS "Basic usage"
290
291 Print the sum and the mean of values from field 1:
292 .PP
293 .nf
294 .RS
295 $ seq 10 | \fBdatamash\fR sum 1 mean 1
296 55 5.5
297 .RE
298 .fi
299 .PP
300 Group input based on field 1, and sum values (per group) on field 2:
301 .PP
302 .nf
303 .RS
304 $ cat example.txt
305 A 10
306 A 5
307 B 9
308 B 11
309
310 $ \fBdatamash\fR \-g 1 sum 2 < example.txt
311 A 15
312 B 20
313
314 $ \fBdatamash\fR groupby 1 sum 2 < example.txt
315 A 15
316 B 20
317 .RE
318 .fi
319 .PP
320
321 Unsorted input must be sorted (with '\-s'):
322 .PP
323 .nf
324 .RS
325 $ cat example.txt
326 A 10
327 C 4
328 B 9
329 C 1
330 A 5
331 B 11
332
333 $ \fBdatamash\fR \-s \-g1 sum 2 < example.txt
334 A 15
335 B 20
336 C 5
337 .RE
338 .fi
339 .PP
340
341 Which is equivalent to:
342 .PP
343 .nf
344 .RS
345 $ cat example.txt | sort \-k1,1 | \fBdatamash\fR \-g 1 sum 2
346 .RE
347 .fi
348
349
350
351 .SS "Header lines"
352 .PP
353 Use \fB\-H\fR \fB(\-\-headers)\fR if the input file has a header line:
354 .PP
355 .nf
356 .RS
357 # Given a file with student name, field, test score...
358 $ head \-n5 scores_h.txt
359 Name Major Score
360 Shawn Engineering 47
361 Caleb Business 87
362 Christian Business 88
363 Derek Arts 60
364
365 # Calculate the mean and standard deviation for each major
366 $ \fBdatamash\fR \-\-sort \-\-headers \-\-group 2 mean 3 pstdev 3 < scores_h.txt
367
368 (or use short form)
369
370 $ \fBdatamash\fR \-sH \-g2 mean 3 pstdev 3 < scores_h.txt
371
372 (or use named fields)
373
374 $ \fBdatamash\fR \-sH \-g Major mean Score pstdev Score < scores_h.txt
375 GroupBy(Major) mean(Score) pstdev(Score)
376 Arts 68.9 10.1
377 Business 87.3 4.9
378 Engineering 66.5 19.1
379 Health-Medicine 90.6 8.8
380 Life-Sciences 55.3 19.7
381 Social-Sciences 60.2 16.6
382 .RE
383 .fi
384 .PP
385
386 Field names must be escaped with a backslash if they start with a digit
387 or contain special characters (dash/minus, colons, commas).
388 Note the interplay between escaping with backslash and shell quoting.
389 The following equivalent command sum the values of a field named "FOO-BAR":
390
391 .PP
392 .nf
393 .RS
394 $ datamash -H sum FOO\\\\\-BAR < input.txt
395 $ datamash -H sum 'FOO\\\-BAR' < input.txt
396 $ datamash -H sum "FOO\\\\\-BAR" < input.txt
397 .RE
398 .fi
399 .PP
400
401
402
403 .SS "Skipping comment lines"
404 Use \fB\-C\fR \fB(\-\-skip\-comments)\fR to skip lines starting with '#'
405 or '\;' characters (and optional whitespace before them):
406 .PP
407 .nf
408 .RS
409 $ cat in.txt
410 #foo 3
411 bar 5
412 ;baz 7
413
414 $ datamash sum 2 < in.txt
415 15
416
417 $ datamash \-C sum 2 < in.txt
418 5
419 .RE
420 .fi
421 .PP
422
423
424
425 .SS "Multiple fields"
426
427 Use comma or dash to specify multiple fields. The following are equivalent:
428 .nf
429 .RS
430 $ seq 9 | paste \- \- \-
431 1 2 3
432 4 5 6
433 7 8 9
434
435 $ seq 9 | paste \- \- \- | datamash sum 1 sum 2 sum 3
436 12 15 18
437
438 $ seq 9 | paste \- \- \- | datamash sum 1,2,3
439 12 15 18
440
441 $ seq 9 | paste \- \- \- | datamash sum 1-3
442 12 15 18
443 .RE
444 .fi
445 .PP
446
447
448 .SS "Rounding"
449 The following demonstrate the different rounding operations:
450 .nf
451 .RS
452 .RE
453 .\" NOTE: The weird spacing/alignment is due to extract backslash
454 .\" characters. Modify with caution.
455 $ ( echo X ; seq \-1.25 0.25 1.25 ) \\
456 | datamash \-\-full \-H round 1 ceil 1 floor 1 trunc 1 frac 1
457
458 X round(X) ceil(X) floor(X) trunc(X) frac(X)
459 \-1.25 \-1 \-1 \-2 \-1 \-0.25
460 \-1.00 \-1 \-1 \-1 \-1 0
461 \-0.75 \-1 0 \-1 0 \-0.75
462 \-0.50 \-1 0 \-1 0 \-0.5
463 \-0.25 0 0 \-1 0 \-0.25
464 0.00 0 0 0 0 0
465 0.25 0 1 0 0 0.25
466 0.50 1 1 0 0 0.5
467 0.75 1 1 0 0 0.75
468 1.00 1 1 1 1 0
469 1.25 1 2 1 1 0.25
470 .fi
471 .PP
472
473
474
475 .SS "Reversing fields"
476 .PP
477 .nf
478 .RS
479 $ seq 6 | paste \- \- | \fBdatamash\fR reverse
480 2 1
481 4 3
482 6 5
483 .RE
484 .fi
485 .PP
486
487
488
489 .SS "Transposing a file"
490 .PP
491 .nf
492 .RS
493 $ seq 6 | paste \- \- | \fBdatamash\fR transpose
494 1 3 5
495 2 4 6
496 .RE
497 .fi
498 .PP
499
500
501
502 .SS "Removing Duplicated lines"
503 Remove lines with duplicate key value from field 1
504 (Unlike \fBfirst\fR,\fBlast\fR operations, \fBrmdup\fR is much faster and
505 does not require sorting the file with \-s):
506 .PP
507 .nf
508 .RS
509 # Given a list of files and sample IDs:
510 $ cat INPUT
511 SampleID File
512 2 cc.txt
513 3 dd.txt
514 1 ab.txt
515 2 ee.txt
516 3 ff.txt
517
518 # Remove lines with duplicated Sample-ID (field 1):
519 $ \fBdatamash\fR rmdup 1 < INPUT
520
521 # or use named field:
522 $ \fBdatamash\fR \-H rmdup SampleID < INPUT
523 SampleID File
524 2 cc.txt
525 3 dd.txt
526 1 ab.txt
527 .RE
528 .fi
529 .PP
530
531
532 .SS "Checksums"
533 Calculate the sha1 hash value of each TXT file,
534 after calculating the sha1 value of each file's content:
535 .PP
536 .nf
537 .RS
538 $ sha1sum *.txt | datamash -Wf sha1 2
539 .RE
540 .fi
541 .PP
542
543
544 .SS "Check file structure"
545 Check the structure of the input file: ensure all lines
546 have the same number of fields, or expected number of lines/fields:
547 .PP
548 .nf
549 .RS
550 $ seq 10 | paste \- \- | datamash check && echo ok || echo fail
551 5 lines, 2 fields
552 ok
553
554 $ seq 13 | paste \- \- \- | datamash check && echo ok || echo fail
555 line 4 (3 fields):
556 10 11 12
557 line 5 (2 fields):
558 13
559 datamash: check failed: line 5 has 2 fields (previous line had 3)
560 fail
561
562 $ seq 10 | paste \- \- | datamash check 2 fields 5 lines
563 5 lines, 2 fields
564
565 $ seq 10 | paste \- \- | datamash check 4 fields
566 line 1 (2 fields):
567 1 2
568 datamash: check failed: line 1 has 2 fields (expecting 4)
569
570 $ seq 10 | paste \- \- | datamash check 7 lines
571 datamash: check failed: input had 5 lines (expecting 7)
572 .RE
573 .fi
574 .PP
575
576
577
578 .SS "Cross-Tabulation"
579 Cross-tabulation compares the relationship between two fields.
580 Given the following input file:
581 .nf
582 .RS
583 $ cat input.txt
584 a x 3
585 a y 7
586 b x 21
587 a x 40
588 .RE
589 .fi
590 .PP
591 Show cross-tabulation between the first field (a/b) and the second field
592 (x/y) - counting how many times each pair appears (note: sorting is required):
593 .PP
594 .nf
595 .RS
596 $ \fBdatamash\fR \-s crosstab 1,2 < input.txt
597 x y
598 a 2 1
599 b 1 N/A
600 .RE
601 .fi
602 .PP
603 An optional grouping operation can be used instead of counting:
604 .PP
605 .nf
606 .RS
607 .PP
608 $ \fBdatamash\fR \-s crosstab 1,2 sum 3 < input.txt
609 x y
610 a 43 7
611 b 21 N/A
612
613 $ \fBdatamash\fR \-s crosstab 1,2 unique 3 < input.txt
614 x y
615 a 3,40 7
616 b 21 N/A
617 .RE
618 .fi
619 .PP
620
621
622 .SS "Binning numeric values"
623 Bin input values into buckets of size 5:
624 .PP
625 .nf
626 .RS
627 $ ( echo X ; seq \-10 2.5 10 ) \\
628 | \fBdatamash\fR \-H \-\-full bin:5 1
629 X bin(X)
630 \-10.0 \-10
631 \-7.5 \-10
632 \-5.0 \-5
633 \-2.5 \-5
634 0.0 0
635 2.5 0
636 5.0 5
637 7.5 5
638 10.0 10
639 .RE
640 .fi
641 .PP
642
643
644 .SS "Binning string values"
645 Hash any input value into a numeric integer.
646 A typical usage would be to split an input file
647 into N chunks, ensuring that all values of a certain key will
648 be stored in the same chunk:
649 .PP
650 .nf
651 .RS
652 $ cat input.txt
653 PatientA 10
654 PatientB 11
655 PatientC 12
656 PatientA 14
657 PatientC 15
658
659 .RE
660
661 Each patient ID is hashed into a bin between 0 and 9
662 and printed in the last field:
663
664 .RS
665
666 $ \fBdatamash\fR \-\-full strbin 1 < input.txt
667 PatientA 10 5
668 PatientB 11 6
669 PatientC 12 7
670 PatientA 14 5
671 PatientC 15 7
672
673 .RE
674
675 Splitting the input into chunks can be done with awk:
676
677 .RS
678
679 $ cat input.txt \\
680 | \fBdatamash\fR \-\-full strbin 1 \\
681 | awk '{print > $NF ".txt"}'
682
683 .RE
684 .fi
685 .PP
686
687
688 .SS "Extracting numbers with getnum"
689 The 'getnum' operation extracts a numeric value from the field:
690
691 .PP
692 .nf
693 .RS
694 $ echo zoom-123.45xyz | \fBdatamash\fR getnum 1
695 123.45
696 .RE
697
698 \fBgetnum\fR accepts an optional single-letter \fBTYPE\fR option:
699 .nf
700 getnum:n - natural numbers (positive integers, including zero)
701 getnum:i - integers
702 getnum:d - decimal point numbers
703 getnum:p - positive decimal point numbers (this is the default)
704 getnum:h - hex numbers
705 getnum:o - octal numbers
706 .fi
707
708 .PP
709 Examples:
710 .nf
711 .RS
712 $ echo zoom-123.45xyz | \fBdatamash\fR getnum 1
713 123.45
714
715 $ echo zoom-123.45xyz | \fBdatamash\fR getnum:n 1
716 123
717
718 $ echo zoom-123.45xyz | \fBdatamash\fR getnum:i 1
719 -123
720
721 $ echo zoom-123.45xyz | \fBdatamash\fR getnum:d 1
722 123.45
723
724 $ echo zoom-123.45xyz | \fBdatamash\fR getnum:p 1
725 -123.45
726
727 # Hex 0x123 = 291 Decimal
728 $ echo zoom-123.45xyz | \fBdatamash\fR getnum:h 1
729 291
730
731 # Octal 0123 = 83 Decimal
732 $ echo zoom-123.45xyz | \fBdatamash\fR getnum:o 1
733 83
734 .RE
735
736
737
738 [ADDITIONAL INFORMATION]
739 See
740 .UR https://www.gnu.org/software/datamash
741 GNU Datamash Website (https://www.gnu.org/software/datamash)