matrix invert routine

Barry Shein bzs at world.std.com
Fri Nov 10 10:33:41 AEST 1989


>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

I'm too lazy to do all the work but here's the basic attack, it can
easily be done in a shell loop:

Assume you have 3 columns as shown:

	#!/bin/sh
	cp /dev/null outfile
	for i in 1 2 3
	do
		cut -f$i datafile | tr '\012' '\011' >> outfile
		echo '' >> outfile
	end
	ed - outfile <<'EOF'
	1,$s/.$//
	w
	q
	EOF

Explanation: "cut" cuts vertical fields separated by tabs (or a
specified character, see man page), the rest is pretty easy. "Tr"
replaces all the newlines with tabs thus flattening the column you cut
into a row, the echo puts a newline on the end of each line.  The ed
script fixes the fact that you ended up with an extra tab on each line
(the very last newline got changed to a tab.)

There's probably some other way to do this (avoiding the final ed
script) but this will certainly work and I think that's what you were
after.
-- 
        -Barry Shein

Software Tool & Die, Purveyors to the Trade         | bzs at world.std.com
1330 Beacon St, Brookline, MA 02146, (617) 739-0202 | {xylogics,uunet}world!bzs



More information about the Comp.unix.questions mailing list