简体   繁体   English

如何在jq中为CSV导出添加标题?

[英]How to add a header to CSV export in jq?

I'm taking a modified command from the jq tutorial : 我正在从jq 教程中修改命令:

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: 哪个csv导出很好,但缺少标题作为顶部:

"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? 如何在顶部添加标题(在本例中为message,name )? (I know it's possible manually, but how to do it within jq ?) (我知道它可以手动,但如何在jq内完成?)

只需在值前面的数组中添加标题文本即可。

["Commit Message","Committer Name"], (.[].commit | [.message,.committer.name]) | @csv

Based on Anton's comments on Jeff Mercado's answer, this snippet will get the key names of the properties of the first element and output them as an array before the rows, thus using them as headers. 根据Anton对Jeff Mercado的回答的评论,这段代码将获取第一个元素的属性的关键名称,并在行之前将它们作为数组输出,从而将它们用作标题。 If different rows have different properties, then it won't work well; 如果不同的行具有不同的属性,那么它将无法正常工作; then again, neither would the resulting CSV. 然后再次,结果CSV也不会。

map({message: .commit.message, name: .commit.committer.name}) | (.[0] | to_entries | map(.key)), (.[] | [.[]]) | @csv

While I fully realize OP was looking for a purely jq answer, I found this question looking for any answer. 虽然我完全意识到OP正在寻找纯粹的jq答案,但我发现这个问题正在寻找任何答案。 So, let me offer one I found (and found useful) to others like me. 所以,让我提供一个我发现(并发现有用)给我这样的人。

  1. sudo apt install moreutils - if you don't have them yet. sudo apt install moreutils - 如果你还没有它们。 Moreutils website . Moreutils网站
  2. echo "Any, column, name, that, is, not, in, your, json, object" | cat - your.csv | sponge your.csv

Disadvantages: requires moreutils package, is not just jq -reliant, so some would understandably say less elegant. 缺点:需要moreutils包,不仅仅是jq -reliant,所以有些人会说不那么优雅。

Advantages: you choose your headers, not your JSON keys. 优点:您选择标题,而不是JSON键。 Also, pure jq ways are bothered by the sorting of the keys, depending on your version . 此外, jq方式会受到键的排序的影响, 具体取决于您的版本

How does it work? 它是如何工作的?

  1. echo outputs your header echo输出你的标题
  2. cat - takes echo output from stdin (cause -) and conCATenates it with your csv file cat - 从stdin(cause - )获取e​​cho输出并使用csv文件对其进行conCATenate
  3. sponge waits until that is done and writes the result to same file, overwriting it. 海绵等待直到完成并将结果写入同一文件,覆盖它。

But you could do it with tee without having to install any packages! 但你可以用tee做到这一点,而无需安装任何包!

No, you could not, as Kos excellently demonstrates here . 不,你不能,正如科斯在这里所展示的那样 Not unless you're fine with loosing your csv at some point. 除非你在某些时候失去你的csv是好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM