请在packer_manifest.json
中考虑以下JSON:
{
"builds": [
{
"name": "amazon-ebs",
"builder_type": "amazon-ebs",
"build_time": 1490743286,
"files": null,
"artifact_id": "us-east-1:ami-ae4af3b8",
"packer_run_uuid": "76afaf47-2ac2-8b50-da43-ea5bff541ae6"
},
{
"name": "amazon-ebs",
"builder_type": "amazon-ebs",
"build_time": 1490744203,
"files": null,
"artifact_id": "us-east-1:ami-994ff68f",
"packer_run_uuid": "d5655333-4240-bbf5-8b08-94be7606536d"
},
{
"name": "amazon-ebs",
"builder_type": "amazon-ebs",
"build_time": 1490744956,
"files": null,
"artifact_id": "us-east-1:ami-e241f8f4",
"packer_run_uuid": "5f7efb09-4b45-9239-ffe6-903a0aed26e4"
},
{
"name": "amazon-ebs",
"builder_type": "amazon-ebs",
"build_time": 1490745669,
"files": null,
"artifact_id": "us-east-1:ami-4046ff56",
"packer_run_uuid": "69fd6d30-fdfd-6778-54ac-338abbea2d88"
},
{
"name": "amazon-ebs",
"builder_type": "amazon-ebs",
"build_time": 1490746077,
"files": null,
"artifact_id": "us-east-1:ami-dc3b82ca",
"packer_run_uuid": "4c74241d-f7e2-bdad-6fa7-3fb84e56c9b3"
},
{
"name": "amazon-ebs",
"builder_type": "amazon-ebs",
"build_time": 1490806537,
"files": null,
"artifact_id": "us-east-1:ami-2c4cf43a",
"packer_run_uuid": "afcbe826-1a0a-7042-35cb-3e3c2569b48b"
},
{
"name": "amazon-ebs",
"builder_type": "amazon-ebs",
"build_time": 1491921968,
"files": null,
"artifact_id": "us-east-1:ami-69a8207f",
"packer_run_uuid": "57ad32f1-6291-7a60-5ee9-3bf46aacd288"
},
{
"name": "amazon-ebs",
"builder_type": "amazon-ebs",
"build_time": 1491940019,
"files": null,
"artifact_id": "us-east-1:ami-035ad215",
"packer_run_uuid": "a0124439-8002-9d13-59bf-43fdcef6eb5e"
},
{
"name": "amazon-ebs",
"builder_type": "amazon-ebs",
"build_time": 1491940543,
"files": null,
"artifact_id": "us-east-1:ami-7e5fd768",
"packer_run_uuid": "42a0f104-460f-789a-6a86-37ea6b9fbf93"
},
{
"name": "amazon-ebs",
"builder_type": "amazon-ebs",
"build_time": 1491941479,
"files": null,
"artifact_id": "us-east-1:ami-0252da14",
"packer_run_uuid": "13fd9737-e41f-ce82-b991-b363fae971b5"
},
{
"name": "amazon-ebs",
"builder_type": "amazon-ebs",
"build_time": 1492026265,
"files": null,
"artifact_id": "us-east-1:ami-a3159eb2",
"packer_run_uuid": "83d55673-620e-05ee-2983-24cab0a009d5"
}
],
"last_run_uuid": "83d55673-620e-05ee-2983-24cab0a009d5"
}
我正在尝试仅使用us-east-1:ami-a3159eb2
提取字符串jq
。
我想我需要执行这样的事情:
jq -r '. | .builds[].artifact_id(where "packer_run_uuid" == .last_run_uuid)'
答案 0 :(得分:3)
使用select
过滤特定文件:
jq -r '.last_run_uuid as $uuid | .builds[] | select(.packer_run_uuid == $uuid) | .artifact_id' data.json
答案 1 :(得分:1)
您的答案中不需要初始.|
。
由于" last_run_uuid" key位于顶层,首先提取其值是值得的,如下面的过滤器所示:
.["last_run_uuid"] as $last
| .builds[]
| select( $last == .["packer_run_uuid"] )
| .artifact_id
.last_run_uuid as $last | .builds[] | select( $last == .packer_run_uuid) | .artifact_id
{
"builds": [
{
"name": "string",
"builder_type": "string",
"build_time": "number",
"files": "null",
"artifact_id": "string",
"packer_run_uuid": "string"
}
],
"last_run_uuid": "string"
}