Next: , Previous: , Up: Usage Examples   [Contents][Index]


5.5 Reverse and Transpose

Transpose

Use transpose to swap rows and columns in a file:

$ cat input.txt
Sample   Year   Count
A        2014   1002
B        2013    990
C        2014   2030
D        2014    599

$ datamash transpose < input.txt
Sample  A       B       C       D
Year    2014    2013    2014    2014
Count   1002    990     2030    599

By default, transpose verifies the input has the same number of fields in each line, and fails with an error otherwise:

$ cat input.txt
Sample   Year   Count
A        2014   1002
B        2013
C        2014   2030
D        2014    599


$ datamash transpose < input1.txt
datamash: transpose input error: line 3 has 2 fields (previous lines had 3);
see --help to disable strict mode

Use --no-strict to allow missing values:

$ datamash --no-strict transpose < input1.txt
Sample  A       B        C        D
Year    2014    2013     2014     2014
Count   1002    N/A      2030     599

Use --filler to set the missing-field filler value:

$ datamash --no-strict --filler XYZ transpose < input1.txt
Sample  A       B        C        D
Year    2014    2013     2014     2014
Count   1002    XYZ      2030     599

Reverse

Use reverse to reverse the fields order in a file:

$ cat input.txt
Sample   Year   Count
A        2014   1002
B        2013    990
C        2014   2030
D        2014    599

$ datamash reverse < input.txt
Count   Year    Sample
1002    2014    A
990     2013    B
2030    2014    C
599     2014    D

By default, reverse verifies the input has the same number of fields in each line, and fails with an error otherwise. Use --no-strict to disable this behavior (see section above for an example).

Combining Reverse and Transpose

Reverse and Transpose can be combined to achieve various manipulations. (reminder: tac can be used to reverse lines in a file):

$ cat input.txt
A       1       xx
B       2       yy
C       3       zz


$ tac input.txt
C       3       zz
B       2       yy
A       1       xx


$ tac input.txt | datamash reverse
zz      3       C
yy      2       B
xx      1       A


$ cat input.txt | datamash reverse | datamash transpose
xx      yy      zz
1       2       3
A       B       C

$ tac input.txt | datamash reverse | datamash transpose
zz      yy      xx
3       2       1
C       B       A

Next: Groupby on /etc/passwd, Previous: Column Ranges, Up: Usage Examples   [Contents][Index]