Xref: utzoo alt.sources.wanted:8 comp.sources.wanted:9362 comp.unix.questions:17573 Path: utzoo!utgpu!utstat!jarvis.csri.toronto.edu!rutgers!cs.utexas.edu!uunet!lotus!esegue!johnl From: johnl@esegue.segue.boston.ma.us (John R. Levine) Newsgroups: alt.sources.wanted,comp.sources.wanted,comp.unix.questions Subject: Re: matrix transpose [not invert] routine Message-ID: <1989Nov10.160534.6717@esegue.segue.boston.ma.us> Date: 10 Nov 89 16:05:34 GMT References: <1612@xn.LL.MIT.EDU> Reply-To: johnl@esegue.segue.boston.ma.us (John R. Levine) Organization: Segue Software, Cambridge MA Lines: 30 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