panthema / 2007 / stx-btree / stx-btree-0.8.1 / speedtest / transpose.awk (Download File)
#!/usr/bin/gawk -f
# Transpose a matrix: assumes all lines have same number
# of fields

NR == 1 {
    n = NF
    for (i = 2; i <= NF; i++)
	row[i] = $i
    next
}

{
    if (NF > n)
	n = NF
    for (i = 2; i <= NF; i++)
	row[i] = row[i] " " $i
}

END {
    for (i = 2; i <= n; i++)
	print row[i]
}