Transpose

To transpose a N rows x M columns file into a M rows x N column file, one approach is to read the whole file in memory as a bidimensional array, and finally print it transposing rows and columns (the example assumes comma separated fields; adapt to your real data):

BEGIN{FS=","}

{for(i=1;i<=NF;i++)a[NR,i]=$i}
END{
  for(i=1;i<=NF;i++){
    for(j=1;j<=NR;j++){
      line=line sep a[j,i]
      sep=FS
    }
    print line
    line=sep=""
  }
}

NR is our N, and NF is our M here. In the END block, for M times, the output is built line-by-line and after N values are concatenated, the line is printed and set back to empty, ready for building the next line.