卷曲输出到CSV-BASH

时间:2018-04-16 10:14:53

标签: json bash csv curl

使用此命令我得到JSON输出:

curl -s -H "Accept: application/json" -X GET "http://192.168.253.21:4440/api/20/project/test/executions?authtoken=kH44NoX35bp1zxohgkMtsOIC9H9tw6UI" | jq -r '.|[.executions[] | select(.job.name != null) | select(.job.name|contains("JIRA_Create_Subtask")) ] | sort_by(.id) | reverse | .[0] | [.status, .job.name, ."date-started".date, ."date-ended".date, .job.project]' > /tmp/1.txt

cat /tmp/1.txt
[
  "succeeded",
  "JIRA_Create_Subtask",
  "2018-04-16T10:00:00Z",
  "2018-04-16T10:00:02Z",
  "test"
]

如何以csv格式获取此输出:

"succeeded","JIRA_Create_Subtask","2018-04-16T10:00:00Z","2018-04-16T10:00:02Z","test"

2 个答案:

答案 0 :(得分:1)

You can use @csv format in jq:

curl -s -H "Accept: application/json" -X GET "http://192.168.253.21:4440/api/20/project/test/executions?authtoken=kH44NoX35bp1zxohgkMtsOIC9H9tw6UI" |
jq -r '.|[.executions[] |
   select(.job.name != null) |
   select(.job.name|contains("JIRA_Create_Subtask")) ] |
   sort_by(.id) | reverse | .[0] |
   [.status, .job.name, ."date-started".date, ."date-ended".date, .job.project] |
   @csv'

答案 1 :(得分:0)

如果您的值中没有任何空格或方括号,则可以使用tr

cat 1.txt | tr -d '[]\n '

tr删除任何[],cariage返回或空格字符。