spock@iham1.UUCP (Ed Weiss) (10/29/85)
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 Maybe the way to do this is to have a spreadsheet that can output tbl input. Is there a generally available one to do this? Thanks- -- Ed Weiss ihnp4!iham1!spock --> Live Long and Prosper <--
vsh@pixel.UUCP (vsh) (10/30/85)
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