This looks like a useful tool for processing CSV files:
https://www.gnu.org/software/datamash/examples/