我需要获取该公司创建的亚马逊图像列表。这将放入一个变量,我将其转换为csv格式并输出到电子表格。
我需要考虑映像ID和附加的卷的大小。
如果我尝试执行以下命令:
cat ab-prod-images.json | jq -r '.Images[] | [.ImageId, ([.BlockDeviceMappings[].Ebs.VolumeSize])] | @csv'
这是我得到的错误:
jq: error (at <stdin>:53471): array ([100,5,10,2...) is not valid in a csv row
我将该命令的输出存储在一个变量中,该变量将用于输出到电子表格。
我想要的是一个用逗号分隔的卷大小列表,该列表显示在电子表格的同一单元格中。
这是我正在使用的json的示例:
{
"Images": [
{
"VirtualizationType": "hvm",
"Description": "TRAN05-12-13-2018",
"Hypervisor": "xen",
"ImageId": "ami-000bd263d51e443f2",
"State": "available",
"BlockDeviceMappings": [{
"DeviceName": "/dev/sda1",
"Ebs": {
"SnapshotId": "snap-0f1076dce3103833f",
"DeleteOnTermination": true,
"VolumeType": "gp2",
"VolumeSize": 100,
"Encrypted": false
}
},
{
"DeviceName": "/dev/sdi",
"Ebs": {
"SnapshotId": "snap-0c7d71a9d3887a048",
"DeleteOnTermination": true,
"VolumeType": "gp2",
"VolumeSize": 5,
"Encrypted": false
}
},
{
"DeviceName": "/dev/sdg",
"Ebs": {
"SnapshotId": "snap-00402196b9e023a72",
"DeleteOnTermination": true,
"VolumeType": "gp2",
"VolumeSize": 10,
"Encrypted": false
}
},
{
"DeviceName": "/dev/sdk",
"Ebs": {
"SnapshotId": "snap-05f65a62735de2846",
"DeleteOnTermination": true,
"VolumeType": "gp2",
"VolumeSize": 20,
"Encrypted": false
}
},
{
"DeviceName": "/dev/sdj",
"Ebs": {
"SnapshotId": "snap-0d350170b9d5c62bb",
"DeleteOnTermination": true,
"VolumeType": "gp2",
"VolumeSize": 40,
"Encrypted": false
}
},
{
"DeviceName": "/dev/sdh",
"Ebs": {
"SnapshotId": "snap-04679d09d89609e79",
"DeleteOnTermination": true,
"VolumeType": "gp2",
"VolumeSize": 30,
"Encrypted": false
}
},
{
"DeviceName": "/dev/sdf",
"Ebs": {
"SnapshotId": "snap-0b5f861fd93da6b20",
"DeleteOnTermination": true,
"VolumeType": "gp2",
"VolumeSize": 5,
"Encrypted": false
}
}
],
"Architecture": "x86_64",
"ImageLocation": "155775729998/AB-P-RIDD-TRAN05-12-13-2018",
"RootDeviceType": "ebs",
"OwnerId": "123456789101",
"RootDeviceName": "/dev/sda1",
"CreationDate": "2018-12-14T01:39:31.000Z",
"Public": false,
"ImageType": "machine",
"Name": "TRAN05-12-13-2018"
}
]
}
这是我要使用的完整完整的json:FULL JSON
如何获取VolumeSizes列表以放入要创建的电子表格的同一单元格中?
答案 0 :(得分:2)
@csv
的输入必须是平面数组,但是您的过滤器(.Images[] | [.ImageId, ([.BlockDeviceMappings[].Ebs.VolumeSize])]
)会生成嵌套数组,因为您将体积大小放入了子数组( [ {{ 1}} ] ):
.BlockDeviceMappings[].Ebs.VolumeSize
因此,请改用以下命令。
$ jq -rc '.Images[] | [.ImageId, ([.BlockDeviceMappings[].Ebs.VolumeSize])]' file.json
["ami-000bd263d51e443f2",[100,5,10,20,40,30,5]]
产生:
jq -r '.Images[] | [ .ImageId, .BlockDeviceMappings[].Ebs.VolumeSize ] | @csv' ab-prod-images.json