datamash --no-strict -t, sum 1 < ragged.csv

Category: Data Science & Analysis

datamash --no-strict -t, sum 1 < ragged.csv

Sum column 1 of a ragged CSV, tolerating missing fields

The --no-strict flag lets datamash accept ragged rows, where some lines have fewer fields than the header. Without it datamash errors out on the first short line; with it, missing trailing fields are treated as empty. This example sums the first field of a messy CSV export where later columns are often blank. For permanently fixing ragged files, xsv fixlengths pads them to a uniform width.
Looking for more? Search all 7,657 commands — works offline, in English or Spanish, and fixes typos.