当我运行此命令以获取图像导入状态时:
aws ec2 describe-import-image-tasks
为我提供了以下示例JSON,但这些都是我导入的所有图像:
{
"ImportImageTasks": [
{
"Architecture": "x86_64",
"ImageId": "ami-99999999",
"ImportTaskId": "import-ami-00000000",
"LicenseType": "BYOL",
"Platform": "Linux",
"Status": "completed"
},
{
"Architecture": "x86_64",
"ImageId": "ami-88888888",
"ImportTaskId": "import-ami-11111111",
"LicenseType": "BYOL",
"Platform": "Linux",
"Status": "completed"
},
{
"Architecture": "x86_64",
"ImageId": "ami-77777777",
"ImportTaskId": "import-ami-22222222",
"LicenseType": "BYOL",
"Platform": "Linux",
"Status": "completed"
}
]
}
我只想要与指定的ImageId匹配的块。像这样:
{
"Architecture": "x86_64",
"ImageId": "ami-77777777",
"ImportTaskId": "import-ami-22222222",
"LicenseType": "BYOL",
"Platform": "Linux",
"Status": "completed"
}
如何使用JQ做到这一点?
答案 0 :(得分:3)
如果您要提取仅一个块:
jq --arg id ami-77777777 '.ImportImageTasks[] | select(.ImageId==$id)'
如果要过滤块,并以相同的格式返回文档,但删除所有其他ImageImportTask
块:
jq --arg id ami-77777777 '.ImportImageTasks |= [.[] | select(.ImageId==$id)]'