Currently the basic filter takes no arguments or options.
Pipe line-based text containing columns of numbers into colsum.
The filter will locate numbers contained in the text by sequential order, and calculate totals.
The output is the ordered sums of the columns contained in the text:
$ echo -e "1,234 5 6\n7 foo 8 bar 9 0\n" | colsum 1241.0 13.0 15.0 0.0
The primary use-case is to select lines in Vim and then pipe through colsum to return the totals.
- In Vim highlight the lines that contain your ordered columns of numbers.
- Type
:to enter command mode. - Type the command
!colsumand lines will be replaced by the sums.
This avoids any vimrc content or plugins and is a lot simpler than the usual awk solutions.
The source can also be quickly customised to suit individual use-cases.
There are several TODOs listed in the docstring. The current implementation is all I needed to get started.
- Text is processed as lines and numerics identified in sequence.
- Each line is then treated as a sequence of numbers.
- Numbers so identified are then summed by columns.
- The output is just the space separated list of sums.
Strings of numerics that are not valid floats will throw an exception.
Given the variety of environmental differences compiling the Python using Cython and place the binary in /usr/local/bin has some simple advantages.
This ensures robust accessibility and independence when starting Vim from inside or outside of virtual environments.
- Install Cython if necessary:
sudo apt install cython - Make the source code available as
pyx:cp colsum.py colsum.pyx - Generate the C file:
cython colsum.pyx --embed - Compile the source:
gcc -Os -I /usr/include/python3.8 -o colsum colsum.c -lpython3.8 -lpthread -lm -lutil -ldl - Install the binary:
sudo mv colsum /usr/local/bin/colsum
Too easy.