Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a header to CSV export in jq?

Tags:

People also ask

Can JQ read CSV?

`jq` can transform CSV to JSON and vice-versa, especially for simple/naive data where simply splitting on `,` is good enough - and where you aren't too bothered by types (e.g. if you don't mind numbers ending up as strings).


I'm taking a modified command from the jq tutorial:

curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5' \ | jq -r -c '.[] | {message: .commit.message, name: .commit.committer.name} | [.[]] | @csv' 

Which does csv export well, but missing the headers as the top:

"Fix README","Nicolas Williams" "README: send questions to SO and Freenode","Nicolas Williams" "usage() should check fprintf() result (fix #771)","Nicolas Williams" "Use jv_mem_alloc() in compile.c (fix #771)","Nicolas Williams" "Fix header guards (fix #770)","Nicolas Williams" 

How can I add the header (in this case message,name) at the top? (I know it's possible manually, but how to do it within jq?)


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!