输入json:
[
{
"id": "1e9b2215-9efd-67cc-9113-2b21c74f09d3",
"login": "u0154",
"name": "Roman ABC",
"role": "User",
"groups": [
{
"id": "1e99833f-9876-65d0-9d24-237228b9d9e0",
"name": "AGE"
}
],
"disabled": false,
"lLoginTime": 1567158950468,
"lLoginFrom": "192.168.100.11"
},
{
"id": "1e9b2215-9c2b-64f4-a0f1-47abdab7ae5f",
"login": "u0155",
"name": "Tomas CDE",
"role": "User",
"groups": [
{
"id": "1e8dc82a-596f-623c-8bc5-100000000000",
"name": "ZZZ"
},
{
"id": "1e99234f-9876-65d0-9d24-534528b9d9e0",
"name": "NTE"
}
],
"disabled": false,
"lLoginTime": 1566302653093,
"lLoginFrom": "192.168.100.16"
}
]
我需要打印制表符分隔的 .id、.login 以及所有用 | 分隔的用户组(竖线)或逗号
我现在拥有的:
$ jq -r '.[]|[.id, .login, ([.groups[].name]|@tsv)]|@tsv' test.json
1e9b2215-9efd-67cc-9113-2b21c74f09d3 u0154 AGE
1e9b2215-9c2b-64f4-a0f1-47abdab7ae5f u0155 ZZZ\tNTE
我需要修改过滤器,以便最后一列上的组用空格分隔,或者|
代替当前的组\t
答案1
只需使用join()
而不是@tsv
:
$ jq -r '.[]|[.id, .login, ([.groups[].name]|join("|"))]|@tsv' test.json
1e9b2215-9efd-67cc-9113-2b21c74f09d3 u0154 AGE
1e9b2215-9c2b-64f4-a0f1-47abdab7ae5f u0155 ZZZ|NTE