An efficient way to transpose a file in Bash

前端 未结 29 2129
时光说笑
时光说笑 2020-11-22 03:30

I have a huge tab-separated file formatted like this

X column1 column2 column3
row1 0 1 2
row2 3 4 5
row3 6 7 8
row4 9 10 11

I would like t

29条回答
  •  死守一世寂寞
    2020-11-22 03:41

    Another awk solution and limited input with the size of memory you have.

    awk '{ for (i=1; i<=NF; i++) RtoC[i]= (RtoC[i]? RtoC[i] FS $i: $i) }
        END{ for (i in RtoC) print RtoC[i] }' infile
    

    This joins each same filed number positon into together and in END prints the result that would be first row in first column, second row in second column, etc. Will output:

    X row1 row2 row3 row4
    column1 0 3 6 9
    column2 1 4 7 10
    column3 2 5 8 11
    

提交回复
热议问题