【发布时间】:2017-11-09 18:18:32
【问题描述】:
我正在慢慢掌握 jq 的工作原理,但距离掌握它还很遥远。 现在我处于一种我已经设法得到我想要的东西但没有以我想要的方式展示它的情况。我敢肯定这很简单,但我想念它......
这是我要解析的 JSON 示例:
{
"sites": [
{
"site_id": 123456,
"status": "configured",
"domain": "www.domain.com",
"account_id": 654321,
"security": {
"waf": {
"rules": [
{
"action": "block_request",
"action_text": "Block",
"id": "sqli",
"name": "SQLi"
},
{
"action": "block_request",
"action_text": "Block",
"id": "xss",
"name": "XSS"
},
{
"action": "alert",
"action_text": "Alert",
"id": "path_vector",
"name": "Path Vector"
}
]
}
}
}
],
"res": 0,
"res_message": "OK",
"debug_info": {
"id-info": "9123"
}
}
我只需要一些细节并将它们以 CSV 格式保存,这是我目前所做的:
cat test.json | jq -r '.sites [] | [.site_id,.domain],(.security.waf.rules[] | [.action_text]) | @csv'
这是我得到的输出:
123456,"www.domain.com"
"Block"
"Block"
"Alert"
还不错,但我正在寻找的是这样的:
123456,"www.domain.com","Block","Block","Alert"
同样的结果,只是显示在一行中。 我浏览了手册页并摆弄了一段时间无济于事。 是否可以这样做,或者我需要一个不同的工具来操作它?
提前致谢!
【问题讨论】:
标签: json csv export-to-csv jq