#! /bin/bash

# Objetivo: Script genérico para transpor um arquivo com linhas em colunas.
# 
# Uso: 
# $ ./transpose.sh file.txt > file_transp.txt
#
# Resultado:
#
# Arquivo original: 
# file.txt 
# a a a a a
# b b b b b
# c c c c c
#
# Arquivo transposto: 
# file_transp.txt
# a b c
# a b c
# a b c
# a b c
# a b c
#
# Referência: https://stackoverflow.com/questions/1729824/an-efficient-way-to-transpose-a-file-in-bash

awk '
{ 
    for (i=1; i<=NF; i++)  {
        a[NR,i] = $i
    }
}
NF>p { p = NF }
END {    
    for(j=1; j<=p; j++) {
        str=a[1,j]
        for(i=2; i<=NR; i++){
            str=str" "a[i,j];
        }
        print str
    }
}' ${1}
