DATAMASH(1)
π NAME π
datamash β command-line calculations. It's the manual page for datamash, from the User Commands section. Bestie, this tool does numeric and string operations on your data, and it's lowkey iconic. β¨
β¨ SYNOPSIS β¨
datamash [OPTION] op [fld] [op fld ...]
πΈ DESCRIPTION πΈ
Performs numeric/string operations on input from stdin.
op is the operation to perform. If a primary operation is used, it must be listed first, optionally followed by other operations. fld is the input field to use. fld can be a number (1=first field), or a field name when using the -H or --header-in options. Multiple fields can be listed with a comma (e.g. 1,6,8). A range of fields can be listed with a dash (e.g. 2-8). Use colons for operations which require a pair of fields (e.g. pcov 2:6).
- Primary operations:
groupby,crosstab,transpose,reverse,check - Line-Filtering operations:
rmdup - Per-Line operations:
base64,debase64,md5,sha1,sha224,sha256,sha384,sha512,bin,strbin,round,floor,ceil,trunc,frac,dirname,basename,barename,extname,getnum,cut - Numeric Grouping operations:
sum,min,max,absmin,absmax,range - Textual/Numeric Grouping operations:
count,first,last,rand,unique,collapse,countunique - Statistical Grouping operations:
mean,geomean,harmmean,trimmean,median,q1,q3,iqr,perc,mode,antimode,pstdev,sstdev,pvar,svar,ms,rms,mad,madraw,pskew,sskew,pkurt,skurt,dpo,jarque,scov,pcov,spearson,ppearson,dotprod
π OPTIONS π
π Grouping Options π
-C,--skip-commentsβ skip comment lines (starting with#or;and optional whitespace)-f,--fullβ print entire input line before op results (default: print only the grouped keys)
This option is only sensible for linewise operations. Other uses are deprecated and will be removed in a future version of GNU Datamash.
-g,--group=X[,Y,Z]β group via fields X,[Y,Z]; equivalent to primary operationgroupby--header-inβ first input line is column headers--header-outβ print column headers as first line-H,--headersβ same as--header-in --header-out--vnlogβ Reads and writes data in the vnlog format. Implies-C -H -W-i,--ignore-caseβ ignore upper/lower case when comparing text; this affects grouping, and string operations-s,--sortβ sort the input before grouping; this removes the need to manually pipe the input throughsort-S,--seedβ set a seed for operations that use randomization-c,--collapse-delimiter=Xβ use X to separate elements incollapseanduniquelists (default: comma)
π File Operation Options π
--no-strictβ allow lines with varying number of fields--filler=Xβ fill missing values with X (defaultN/A)
βοΈ General Options βοΈ
-t,--field-separator=Xβ use X instead of TAB as field delimiter--format=FORMATβ print numeric values with printf style floating-point FORMAT.--output-delimiter=Xβ use X instead as output field delimiter (default: use same delimiter as-t/-W)--narmβ skip NA/NaN values-R,--round=Nβ round numeric output to N decimal places-W,--whitespaceβ use whitespace (one or more spaces and/or tabs) for field delimiters-z,--zero-terminatedβ end lines with 0 byte, not newline--sort-cmd=/path/to/sortβ Alternativesort(1)to use.-h,--helpβ display this help and exit-V,--versionβ output version information and exit
π AVAILABLE OPERATIONS π
π Primary Operations π
Primary operations affect the way the file is processed. If used, the primary operation must be listed first. If primary operation is not listed the entire file is processed - either line-by-line (for per-line operations) or all lines as one group (for grouping operations). See Examples section below.
groupby X,Y,... op fld ...β group the file by given fields. Equivalent to option-g. For each group perform operationopon fieldfld.crosstab X,Y [op fld ...]β cross-tabulate a file by two fields (cross-tabulation is also known as pivot tables). If no operation is specified, counts how many incidents exist of X,Y.transposeβ transpose rows, columns of the input filereverseβ reverse field order in each linecheck [N lines] [N fields]β verify the input file has same number of fields in all lines, or the expected number of lines/fields. number of lines and fields are printed to STDOUT. Exits with non-zero code and prints the offending line if there's a mismatch in the number of lines/ fields.
π§Ή Line-Filtering operations π§Ή
rmdupβ remove lines with duplicated key value
β¨ Per-Line operations β¨
base64β Encode the field as base64debase64β Decode the field as base64, exit with error if invalid base64 stringmd5/sha1/sha224/sha256/sha384/sha512β Calculate md5/sha1/sha224/sha256/sha384/sha512 hash of the field valuebin[:BUCKET-SIZE]β bin numeric values into buckets of size BUCKET-SIZE (defaults to 100).strbin[:BUCKET-SIZE]β hashes the input and returns a numeric integer value between zero and BUCKET-SIZE (defaults to 10).round/floor/ceil/trunc/fracβ numeric rounding operations.round(round half away from zero),floor(round down),ceil(ceiling, round up),trunc(truncate, round towards zero),frac(fraction, return fraction part of a decimal-point value).dirname/basenameβ extract the directory name and the base file name from a given string (same as todirname(1)andbasename(1)).extnameβ extract the extension of the file name (without the.).barenameβ extract the base file name without the extension.getnum[:TYPE]β extract a number from the field. TYPE is optional single letter optionn/i/d/p/h/o(see examples below).cut/echoβ copy input field to output field (similar tocut(1)). Theechocommand is simply an alias tocut.
π’ Numeric Grouping operations π’
sumβ sum the of valuesminβ minimum valuemaxβ maximum valueabsminβ minimum of the absolute valuesabsmaxβ maximum of the absolute valuesrangeβ the values range (max-min)
π Textual/Numeric Grouping operations π
countβ count number of elements in the groupfirstβ the first value of the grouplastβ the last value of the grouprandβ one random value from the groupunique/uniqβ comma-separated sorted list of unique values. Theuniqcommand is simply an alias tounique.collapseβ comma-separated list of all input valuescountuniqueβ number of unique/distinct values
π Statistical Grouping operations π
A p/s prefix indicates the variant: population or sample. Typically, the sample variant is equivalent with GNU R's internal functions (e.g datamash's sstdev operation is equivalent to R's sd() function).
meanβ mean of the valuesgeomeanβ geometric mean of the valuesharmmeanβ harmonic mean of the valuestrimmean[:PERCENT]β trimmed mean of the values. PERCENT should be between 0 and 0.5. (trimmean:0is equivalent tomean.trimmean:0.5is equivalent tomedian).msβ mean square of the valuesrmsβ root mean square of the valuesmedianβ median valueq1β 1st quartile valueq3β 3rd quartile valueiqrβ inter-quartile rangeperc[:PERCENTILE]β percentile value PERCENTILE (defaults to 95).modeβ mode value (most common value)antimodeβ anti-mode value (least common value)pstdev/sstdevβ population/sample standard deviationpvar/svarβ population/sample variancemadβ median absolute deviation, scaled by constant 1.4826 for normal distributionsmadrawβ median absolute deviation, unscaledpskew/sskewβ skewness of the group. values x reported bysskewandpskewoperations:x > 0β positively skewed / skewed right0 > xβ negatively skewed / skewed leftx > 1β highly skewed right1 > x > 0.5β moderately skewed right0.5 > x > -0.5β approximately symmetric-0.5 > x > -1β moderately skewed left-1 > xβ highly skewed left
pkurt/skurtβ excess Kurtosis of the groupjarque/dpoβ p-value of the Jarque-Beta (jarque) and D'Agostino-Pearson Omnibus (dpo) tests for normality:- null hypothesis is normality;
- low p-Values indicate non-normal data;
- high p-Values indicate null-hypothesis cannot be rejected.
pcov/scov [X:Y]β covariance of fields X and Yppearson/spearson [X:Y]β Pearson product-moment correlation coefficient [Pearson's R] of fields X and Ydotprod [X:Y]β Scalar product (aka dot product or Euclidean inner product) of fields X and Y
β¨ EXAMPLES β¨
π Basic usage
ok so first up, the baby example β sum + mean of field 1, no notes:
$ seq 10 | datamash sum 1 mean 1
55 5.5
now group the input by field 1 and sum field 2, per group:
$ cat example.txt
A 10
A 5
B 9
B 11
$ datamash -g 1 sum 2 < example.txt
A 15
B 20
$ datamash groupby 1 sum 2 < example.txt
A 15
B 20
see how -g and groupby are giving the same energy? iconic, no cap.
if your input is not sorted, bestie you have to sort it (with -s):
$ cat example.txt
A 10
C 4
B 9
C 1
A 5
B 11
$ datamash -s -g1 sum 2 < example.txt
A 15
B 20
C 5
and that is literally equivalent to:
$ cat example.txt | sort -k1,1 | datamash -g 1 sum 2
πΈ Header lines
got a header line in your file? that's what -H (--headers) is for:
# Given a file with student name, field, test score...
$ head -n5 scores_h.txt
Name Major Score
Shawn Engineering 47
Caleb Business 87
Christian Business 88
Derek Arts 60
# Calculate the mean and standard deviation for each major
$ datamash --sort --headers --group 2 mean 3 pstdev 3 < scores_h.txt
(or use short form)
$ datamash -sH -g2 mean 3 pstdev 3 < scores_h.txt
(or use named fields)
$ datamash -sH -g Major mean Score pstdev Score < scores_h.txt
GroupBy(Major) mean(Score) pstdev(Score)
Arts 68.9 10.1
Business 87.3 4.9
Engineering 66.5 19.1
Health-Medicine 90.6 8.8
Life-Sciences 55.3 19.7
Social-Sciences 60.2 16.6
so you can point at fields by number or by name, whichever your heart says π
now listen β field names must be escaped with a backslash if they start with a digit or contain special characters (dash/minus, colons, commas). and watch the interplay between escaping with backslash and shell quoting. these three commands are all the same thing and all sum the values of a field named FOO-BAR:
$ datamash -H sum FOO\\-BAR < input.txt
$ datamash -H sum 'FOO\-BAR' < input.txt
$ datamash -H sum "FOO\\-BAR" < input.txt
π Skipping comment lines
use -C (--skip-comments) to skip lines starting with # or ; characters (and optional whitespace before them):
$ cat in.txt
#foo 3
bar 5
;baz 7
$ datamash sum 2 < in.txt
15
$ datamash -C sum 2 < in.txt
5
notice the difference: without -C those comment lines get read as data and it's a whole mess π
π¦ Multiple fields
use a comma or a dash to specify multiple fields. all of these are equivalent:
$ seq 9 | paste - - -
1 2 3
4 5 6
7 8 9
$ seq 9 | paste - - - | datamash sum 1 sum 2 sum 3
12 15 18
$ seq 9 | paste - - - | datamash sum 1,2,3
12 15 18
$ seq 9 | paste - - - | datamash sum 1-3
12 15 18
π Rounding
here's the whole rounding lineup in one shot, so you can see exactly how each one behaves:
$ ( echo X ; seq -1.25 0.25 1.25 ) \
| datamash --full -H round 1 ceil 1 floor 1 trunc 1 frac 1
X round(X) ceil(X) floor(X) trunc(X) frac(X)
-1.25 -1 -1 -2 -1 -0.25
-1.00 -1 -1 -1 -1 0
-0.75 -1 0 -1 0 -0.75
-0.50 -1 0 -1 0 -0.5
-0.25 0 0 -1 0 -0.25
0.00 0 0 0 0 0
0.25 0 1 0 0 0.25
0.50 1 1 0 0 0.5
0.75 1 1 0 0 0.75
1.00 1 1 1 1 0
1.25 1 2 1 1 0.25
π Reversing fields
$ seq 6 | paste - - | datamash reverse
2 1
4 3
6 5
β Transposing a file
$ seq 6 | paste - - | datamash transpose
1 3 5
2 4 6
π Removing Duplicated lines
remove lines with a duplicate key value from field 1. and here's the tea: unlike the first,last operations, rmdup is much faster and does not require sorting the file with -s:
# Given a list of files and sample IDs:
$ cat INPUT
SampleID File
2 cc.txt
3 dd.txt
1 ab.txt
2 ee.txt
3 ff.txt
# Remove lines with duplicated Sample-ID (field 1):
$ datamash rmdup 1 < INPUT
# or use named field:
$ datamash -H rmdup SampleID < INPUT
SampleID File
2 cc.txt
3 dd.txt
1 ab.txt
π§ Checksums
calculate the sha1 hash value of each TXT file, after calculating the sha1 value of each file's content:
$ sha1sum *.txt | datamash -Wf sha1 2
π Check file structure
vibe check your input file: make sure all lines have the same number of fields, or an expected number of lines/fields:
$ seq 10 | paste - - | datamash check && echo ok || echo fail
5 lines, 2 fields
ok
$ seq 13 | paste - - - | datamash check && echo ok || echo fail
line 4 (3 fields):
10 11 12
line 5 (2 fields):
13
datamash: check failed: line 5 has 2 fields (previous line had 3)
fail
$ seq 10 | paste - - | datamash check 2 fields 5 lines
5 lines, 2 fields
$ seq 10 | paste - - | datamash check 4 fields
line 1 (2 fields):
1 2
datamash: check failed: line 1 has 2 fields (expecting 4)
$ seq 10 | paste - - | datamash check 7 lines
datamash: check failed: input had 5 lines (expecting 7)
π Cross-Tabulation
cross-tabulation compares the relationship between two fields. given this input file:
$ cat input.txt
a x 3
a y 7
b x 21
a x 40
show the cross-tabulation between the first field (a/b) and the second field (x/y) β counting how many times each pair shows up (heads up: sorting is required):
$ datamash -s crosstab 1,2 < input.txt
x y
a 2 1
b 1 N/A
and you can drop in an optional grouping operation instead of just counting:
$ datamash -s crosstab 1,2 sum 3 < input.txt
x y
a 43 7
b 21 N/A
$ datamash -s crosstab 1,2 unique 3 < input.txt
x y
a 3,40 7
b 21 N/A
π«Ά Binning numeric values
bin input values into buckets of size 5:
$ ( echo X ; seq -10 2.5 10 ) \
| datamash -H --full bin:5 1
X bin(X)
-10.0 -10
-7.5 -10
-5.0 -5
-2.5 -5
0.0 0
2.5 0
5.0 5
7.5 5
10.0 10
π Binning string values
hash any input value into a numeric integer. a typical usage would be splitting an input file into N chunks, making sure all values of a certain key land in the same chunk:
$ cat input.txt
PatientA 10
PatientB 11
PatientC 12
PatientA 14
PatientC 15
each patient ID gets hashed into a bin between 0 and 9 and printed in the last field:
$ datamash --full strbin 1 < input.txt
PatientA 10 5
PatientB 11 6
PatientC 12 7
PatientA 14 5
PatientC 15 7
and splitting the input into chunks can be done with awk:
$ cat input.txt \
| datamash --full strbin 1 \
| awk '{print > $NF ".txt"}'
π Extracting numbers with getnum
the getnum operation extracts a numeric value from the field:
$ echo zoom-123.45xyz | datamash getnum 1
123.45
getnum accepts an optional single-letter TYPE option:
getnum:nβ natural numbers (positive integers, including zero)getnum:iβ integersgetnum:dβ decimal point numbersgetnum:pβ positive decimal point numbers (this is the default)getnum:hβ hex numbersgetnum:oβ octal numbers
examples, bestie, all of them:
$ echo zoom-123.45xyz | datamash getnum 1
123.45
$ echo zoom-123.45xyz | datamash getnum:n 1
123
$ echo zoom-123.45xyz | datamash getnum:i 1
-123
$ echo zoom-123.45xyz | datamash getnum:d 1
123.45
$ echo zoom-123.45xyz | datamash getnum:p 1
-123.45
# Hex 0x123 = 291 Decimal
$ echo zoom-123.45xyz | datamash getnum:h 1
291
# Octal 0123 = 83 Decimal
$ echo zoom-123.45xyz | datamash getnum:o 1
83
π ADDITIONAL INFORMATION π
and that's the whole page, bestie β you made it to the end π
that little footer sitting at the bottom is where the source, the version and the date live, so go give it a peek if you need the receipts β¨π«Ά
π¦ where this page comes from
- alpine 3.24: datamash-doc 1.9-r1 Β· GPL-3.0-or-later
- arch rolling: datamash 1.9-1 Β· GPL-3.0-only
- debian trixie: datamash 1.9-1 Β· GPL-3.0-or-later
- epel 10: datamash 1.9-1.el10_2 Β· GPL-3.0-or-later
- fedora 44: datamash 1.9-3.fc44 Β· GPL-3.0-or-later
- nixos 26.05: datamash 1.9
- nixpkgs unstable: datamash 1.9
- ubuntu questing: datamash 1.9-1
- ubuntu resolute: datamash 1.9-2 Β· GPL-3.0-or-later
- ubuntu stonking: datamash 1.9-2 Β· GPL-3.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms