wireservice / csvkit

A suite of utilities for converting to and working with CSV, the king of tabular file formats.
https://csvkit.readthedocs.io
MIT License
6.03k stars 603 forks source link

Question: how to work with two headers with csvstack? #1185

Closed vitalyk-multinarity closed 1 year ago

vitalyk-multinarity commented 2 years ago

I have CSV with 2 headers, like this:

KPI,KPI,KPI
kpi1,kpi2,kpi3
1.11,2.22,3.33

and I'd like to add a new column with commit hash.

csvstack -n COMMIT -g 12345 myfile.csv

works almost fine, but I need to generate file like

COMMIT,KPI,KPI,KPI
COMMIT,kpi1,kpi2,kpi3
12345,1.11,2.22,3.33

and not

COMMIT,KPI,KPI,KPI
12345,kpi1,kpi2,kpi3
12345,1.11,2.22,3.33

Is this possible with csvstack?

Thanks, Vitaly

jpmckinney commented 1 year ago

csvkit doesn't have an option to support multiple header lines, and it's unlikely to add this feature, because there is low demand and because it would affect a lot of code.

I suggest using sed to simply change the first cell on the second line, e.g.

csvstack -n COMMIT -g 12345 myfile.csv | sed -r '2s/^[^,]+/COMMIT/'

Depending on your version of sed, you might not need -r.