[alt.sources.wanted] matrix transpose [not invert] routine

johnl@esegue.segue.boston.ma.us (John R. Levine) (11/11/89)

In article <1612@xn.LL.MIT.EDU> rkc@XN.LL.MIT.EDU (rkc) writes:
>I have "spreadsheet-like" data that looks like this:
>	a1 b1 c1
>	a2 b2 c2
>	a3 b3 c3
>and I want to get it in a form like:
>	a1 a2 a3
>	b1 b2 b3
>	c1 c2 c3

There are lots of ways to do it.  My favorite, which works for arbitrarily
large arrays, is to label each datum and sort them:

awk '{  for(i = 1; i <= NF; i++)	# lines of form "row col datum"
		print NR,i,$i
     }' |
sort +1n +0n |				# sort by columns
awk '$2 != lineno {
			if(line != "")
				print line
			lineno = $2
			line = $3
			next
		}
     $2 == lineno { line = line " " $3 }
     END { print line }'
-- 
John R. Levine, Segue Software, POB 349, Cambridge MA 02238, +1 617 864 9650
johnl@esegue.segue.boston.ma.us, {ima|lotus|spdcc}!esegue!johnl
Massachusetts has over 100,000 unlicensed drivers.  -The Globe