Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.1 6/24/83; site pixel.UUCP
Path: utzoo!linus!philabs!cmcl2!harvard!talcott!wjh12!pixel!vsh
From: vsh@pixel.UUCP (vsh)
Newsgroups: net.wanted,net.wanted.sources
Subject: Re: tbl spreadsheet capabilities
Message-ID: <32@pixel.UUCP>
Date: Wed, 30-Oct-85 15:19:07 EST
Article-I.D.: pixel.32
Posted: Wed Oct 30 15:19:07 1985
Date-Received: Sun, 3-Nov-85 07:21:02 EST
References: <473@iham1.UUCP>
Distribution: net
Organization: Pixel Systems Inc., Woburn, Mass.
Lines: 56
Xref: linus net.wanted:6752 net.wanted.sources:1482

In <473@iham1.UUCP> spock@iham1.UUCP (Ed Weiss) writes:

> Does anyone have a preprocessor to tbl to add up
> rows or columns?
> 
> I often find that the last row (column) of my tables
> is a sum.  And every time I change some numbers I need
> to re-add them all to compute the last row (column)
> again.  If anyone has a way to automatically do this
> I would be happy to see it.
> 
> Example:
> 
> 	---------------
> 	| Item 1 | 10 |
> 	| Item 2 | 20 |
> 	| Item 3 | 30 |		I don't want to have to
> 	| ------ | -- |		recompute this line
> 	| Total  | 60 |----->	each time I change one
> 	---------------		of the above lines

I use awk, from vi:

Assume the lines of data are 101 to 103 (a three line table, as above), and
you want to sum columns 2, 3, and 5.  Since you are using tbl, the column
separator is the tab character (indicated by ^I, below).

1) write the data to a temp file (say, foo):

	:101,103w foo

2) position your cursor to just above your totals line.

3) read the output of an awk script (within vi):

	:r ! awk -F'^I' '{t2 += $2; t3 += $3; t5 += $5}
		END {print "^I" t2 "^I" t3 "^I^I" t5}' foo

(do NOT type a newline before "END" -- it's only here for legibility)

If you do this often enough, put the awk commands in a file (say, awk1),
then (within vi) type:

	:r ! awk -f awk1 foo

The file awk1 would be:

	BEGIN	{FS = "^I"}
		{t2 += $2; t3 += $3; t5 += $5}
	END	{print "^I" t2 "^I" t3 "^I^I" t5}' foo

-- 
Steve Harris		|  {allegra|ihnp4|cbosgd|ima|genrad|amd|harvard}!\
Pixel Systems Inc.	|   		wjh12!pixel!vsh
300 Wildwood Street	|
Woburn, MA  01801	|  617-933-7735 x2314