GROUP BY/SUM from shell

前端 未结 6 1515
既然无缘
既然无缘 2020-11-28 11:52

I have a large file containing data like this:

a 23
b 8
a 22
b 1

I want to be able to get this:

a 45
b 9

6条回答
  •  庸人自扰
    2020-11-28 12:22

    This can be easily achieved with the following single-liner:

    cat /path/to/file | termsql "SELECT col0, SUM(col1) FROM tbl GROUP BY col0"
    

    Or.

    termsql -i /path/to/file "SELECT col0, SUM(col1) FROM tbl GROUP BY col0"
    

    Here a Python package, termsql, is used, which is a wrapper around SQLite. Note, that currently it's not upload to PyPI, and also can only be installed system-wide (setup.py is a little broken), like:

    sudo pip install https://github.com/tobimensch/termsql/archive/master.zip
    

提交回复
热议问题