jq 打印 csv,如下所示:
... | [.name, .userId, .groupId] | @csv'
打印什么:
"nodea","0","6"
"nodeb","1","0"
我如何处理它以获得“n/a”而不是 0,并且对于 >0 值适当的注释/注释,例如:
"nodea","n/a","group:6"
"nodeb","user:1","n/a"
在 jq 中可行还是仅在后处理中可行,例如。在 awk 中?谢谢
答案1
jq
解决方案:
样本input.json
:
[
{
"name": "nodea",
"userId": 0,
"groupId": 6
},
{
"name": "nodeb",
"userId": 1,
"groupId": 0
}
]
jq -r 'def pr($k): if .[$k] > 0 then $k+":\(.[$k])" else "n/a" end;
.[] | [ .name, pr("userId"), pr("groupId") ] | @csv' input.json
输出:
"nodea","n/a","groupId:6"
"nodeb","userId:1","n/a"