Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
566 views
in Technique[技术] by (71.8m points)

header - Column Name in the CSV file after converting from JSON using JQ

I am having problems on adding a column on my csv.

$ jq -r '.cveRules[] | [.effect, .id, .description, .expiration.date] | @csv' vulexemptioncmndt.json
"ignore","CVE-2020-8116","Package dot-prop 4.2.0, status: fixed in 5.1.1, 4.2.1","2021-02-27T23:00:00Z"
"ignore","CVE-2020-8252","Package node 10.21.0, status: fixed in 14.9.0, 12.18.4, 10.22.1","2021-02-27T23:00:00Z"
"ignore","CVE-2020-8265","Package node 10.21.0, status: fixed in 10.23.1","2021-02-27T23:00:00Z"

What commands should I add so I can have a result of

"Effect", "ID", "Description", "Expiration Date"
"ignore","CVE-2020-8116","Package dot-prop 4.2.0, status: fixed in 5.1.1, 4.2.1","2021-02-27T23:00:00Z"
"ignore","CVE-2020-8252","Package node 10.21.0, status: fixed in 14.9.0, 12.18.4, 10.22.1","2021-02-27T23:00:00Z"
"ignore","CVE-2020-8265","Package node 10.21.0, status: fixed in 10.23.1","2021-02-27T23:00:00Z"
question from:https://stackoverflow.com/questions/65897260/column-name-in-the-csv-file-after-converting-from-json-using-jq

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you don't mind the header row being treated like any other CSV row, a jq-only solution would be to add an array of the desired header names to your filter like so:

["Effect", "ID", "Description", "Expiration Date"],
(.cveRules[] | [.effect, .id, .description, .expiration.date])
| @csv

If you want more control of the header row, then prepend your filter with the desired header row as a single string.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...