使用 bash 在 Json 文件中打印所有传递的任务的 ID
Prints id of all passed tasks in Json file using bash
我有一个如下所示的 Json 文件,
{
"tasks": [
{
"id": "nightly_1652299200",
"repo": "tx/tx5",
"branch": "dev",
"type": "HealthCheck",
"started_on": "2022-05-11 23:00:00 +0300 EEST",
"state": "Running",
"top_sha": "10143120c12cd506fca5a466a7c6b27e7cb35b18",
"tested_shas": null,
"self_url": "tx/tx5/10143120c12cd506fca5a466a7c6b27e7cb35b18",
"is_public": true
},
{
"id": "1652298808",
"repo": "tx/tx3d",
"branch": "6.3",
"type": "Integration",
"started_on": "2022-05-11 22:53:28 +0300 EEST",
"state": "Passed",
"top_sha": "40ca8db14a8ddd082270a81e895bf01048949ec4",
"tested_shas": null,
"self_url": "tx/tx3d/40ca8db14a8ddd082270a81e895bf01048949ec4",
"is_public": true
},
{
"id": "1652298252",
"repo": "tx/qtmultimedia",
"branch": "dev",
"type": "Integration",
"started_on": "2022-05-11 22:44:12 +0300 EEST",
"state": "Failed",
"top_sha": "d7dd593b6395a33862742a04a23d88b970f7b914",
"tested_shas": null,
"self_url": "qt/qtmultimedia/d7dd593b6395a33862742a04a23d88b970f7b914",
"is_public": true
}
}
我需要使用 bash 脚本找到每个已通过任务的 ID(“状态”:“已通过”)。最好的方法是什么。
使用jq
:
jq -r '.tasks | .[] | select (.state == "Passed") | .id' file.json
我有一个如下所示的 Json 文件,
{
"tasks": [
{
"id": "nightly_1652299200",
"repo": "tx/tx5",
"branch": "dev",
"type": "HealthCheck",
"started_on": "2022-05-11 23:00:00 +0300 EEST",
"state": "Running",
"top_sha": "10143120c12cd506fca5a466a7c6b27e7cb35b18",
"tested_shas": null,
"self_url": "tx/tx5/10143120c12cd506fca5a466a7c6b27e7cb35b18",
"is_public": true
},
{
"id": "1652298808",
"repo": "tx/tx3d",
"branch": "6.3",
"type": "Integration",
"started_on": "2022-05-11 22:53:28 +0300 EEST",
"state": "Passed",
"top_sha": "40ca8db14a8ddd082270a81e895bf01048949ec4",
"tested_shas": null,
"self_url": "tx/tx3d/40ca8db14a8ddd082270a81e895bf01048949ec4",
"is_public": true
},
{
"id": "1652298252",
"repo": "tx/qtmultimedia",
"branch": "dev",
"type": "Integration",
"started_on": "2022-05-11 22:44:12 +0300 EEST",
"state": "Failed",
"top_sha": "d7dd593b6395a33862742a04a23d88b970f7b914",
"tested_shas": null,
"self_url": "qt/qtmultimedia/d7dd593b6395a33862742a04a23d88b970f7b914",
"is_public": true
}
}
我需要使用 bash 脚本找到每个已通过任务的 ID(“状态”:“已通过”)。最好的方法是什么。
使用jq
:
jq -r '.tasks | .[] | select (.state == "Passed") | .id' file.json