前提・実現したいこと
あらかじめ以下のコマンドの結果をjsonファイルで保存しています。
aws ec2 describe-instances
結果のjsonは以下のようになっているとします。この時、jqを使ってInstanceIdとTags[]のKeyの値がName1のValueの値を取得したいのですが、どのようなコマンドをワンライナーで書けますでしょうか?
InstanceによってはTags[]のKeyにName1がないものもあるため、そのValueはnull(何も出力しない)としたいです。
JSON
1{ 2 "Reservations": [ 3 { 4 "ReservationId": "r-********", 5 "Groups": [], 6 "OwnerId": "************", 7 "Instances": [ 8 { 9 "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal", 10 "EbsOptimized": false, 11 "Monitoring": { 12 "State": "disabled" 13 }, 14 "State": { 15 "Code": 80 16 }, 17 "NetworkInterfaces": [ 18 { 19 "PrivateDnsName": "ip-********.ap-northeast-1.compute.internal", 20 "PrivateIpAddresses": [ 21 { 22 "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal", 23 "PrivateIpAddress": "172.**.**.***", 24 "Primary": true 25 } 26 ], 27 "OwnerId": "************", 28 "Groups": [ 29 { 30 "GroupName": "RDP", 31 "GroupId": "sg-********" 32 } 33 ], 34 "PrivateIpAddress": "172.**.**.***", 35 "Description": "", 36 "SourceDestCheck": true, 37 "MacAddress": "************", 38 "Status": "in-use", 39 "NetworkInterfaceId": "eni-************", 40 "VpcId": "vpc-************", 41 "SubnetId": "subnet-************" 42 } 43 ], 44 "PrivateIpAddress": "172.**.**.***", 45 "RootDeviceType": "ebs", 46 "StateTransitionReason": "User initiated (2016-02-24 10:01:43 GMT)", 47 "LaunchTime": "2016-02-24T05:53:59.000Z", 48 "Tags": [ 49 { 50 "Key": "Name", 51 "Value": "test" 52 } 53 { 54 "Key": "Name1", 55 "Value": "test1" 56 } 57 { 58 "Key": "Name2", 59 "Value": "test2" 60 } 61 ], 62 "SubnetId": "subnet-************", 63 "Platform": "windows", 64 "StateReason": { 65 "Message": "Client.UserInitiatedShutdown: User initiated shutdown", 66 "Code": "Client.UserInitiatedShutdown" 67 }, 68 "InstanceId": "i-***********1", 69 "SourceDestCheck": true, 70 "ClientToken": "rduVq************", 71 "AmiLaunchIndex": 0 72 } 73 ] 74 "Instances": [ 75 { 76 "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal", 77 "EbsOptimized": false, 78 "Monitoring": { 79 "State": "disabled" 80 }, 81 "State": { 82 "Code": 80 83 }, 84 "NetworkInterfaces": [ 85 { 86 "PrivateDnsName": "ip-********.ap-northeast-1.compute.internal", 87 "PrivateIpAddresses": [ 88 { 89 "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal", 90 "PrivateIpAddress": "172.**.**.***", 91 "Primary": true 92 } 93 ], 94 "OwnerId": "************", 95 "Groups": [ 96 { 97 "GroupName": "RDP", 98 "GroupId": "sg-********" 99 } 100 ], 101 "PrivateIpAddress": "172.**.**.***", 102 "Description": "", 103 "SourceDestCheck": true, 104 "MacAddress": "************", 105 "Status": "in-use", 106 "NetworkInterfaceId": "eni-************", 107 "VpcId": "vpc-************", 108 "SubnetId": "subnet-************" 109 } 110 ], 111 "PrivateIpAddress": "172.**.**.***", 112 "RootDeviceType": "ebs", 113 "StateTransitionReason": "User initiated (2016-02-24 10:01:43 GMT)", 114 "LaunchTime": "2016-02-24T05:53:59.000Z", 115 "Tags": [ 116 { 117 "Key": "Name", 118 "Value": "test" 119 } 120 { 121 "Key": "Name3", 122 "Value": "test3" 123 } 124 ], 125 "SubnetId": "subnet-************", 126 "Platform": "windows", 127 "StateReason": { 128 "Message": "Client.UserInitiatedShutdown: User initiated shutdown", 129 "Code": "Client.UserInitiatedShutdown" 130 }, 131 "InstanceId": "i-***********2", 132 "SourceDestCheck": true, 133 "ClientToken": "rduVq************", 134 "AmiLaunchIndex": 0 135 } 136 ] 137 "Instances": [ 138 { 139 "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal", 140 "EbsOptimized": false, 141 "Monitoring": { 142 "State": "disabled" 143 }, 144 "State": { 145 "Code": 80 146 }, 147 "NetworkInterfaces": [ 148 { 149 "PrivateDnsName": "ip-********.ap-northeast-1.compute.internal", 150 "PrivateIpAddresses": [ 151 { 152 "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal", 153 "PrivateIpAddress": "172.**.**.***", 154 "Primary": true 155 } 156 ], 157 "OwnerId": "************", 158 "Groups": [ 159 { 160 "GroupName": "RDP", 161 "GroupId": "sg-********" 162 } 163 ], 164 "PrivateIpAddress": "172.**.**.***", 165 "Description": "", 166 "SourceDestCheck": true, 167 "MacAddress": "************", 168 "Status": "in-use", 169 "NetworkInterfaceId": "eni-************", 170 "VpcId": "vpc-************", 171 "SubnetId": "subnet-************" 172 } 173 ], 174 "PrivateIpAddress": "172.**.**.***", 175 "RootDeviceType": "ebs", 176 "StateTransitionReason": "User initiated (2016-02-24 10:01:43 GMT)", 177 "LaunchTime": "2016-02-24T05:53:59.000Z", 178 "Tags": [ 179 { 180 "Key": "Name", 181 "Value": "test" 182 } 183 { 184 "Key": "Name1", 185 "Value": "test1" 186 } 187 { 188 "Key": "Name4", 189 "Value": "test4" 190 } 191 ], 192 "SubnetId": "subnet-************", 193 "Platform": "windows", 194 "StateReason": { 195 "Message": "Client.UserInitiatedShutdown: User initiated shutdown", 196 "Code": "Client.UserInitiatedShutdown" 197 }, 198 "InstanceId": "i-***********3", 199 "SourceDestCheck": true, 200 "ClientToken": "rduVq************", 201 "AmiLaunchIndex": 0 202 } 203 ] 204 } 205 ] 206}
発生している問題・エラーメッセージ
事前に取得したjsonファイルのファイル名をxxx.jsonとして、以下のコマンドを試しましたが、エラーが発生します。
当方の環境はpowershell6でございます。
Get-Content xxx.json | jq -r '.Reservations[].Instances[] |.InstanceId, .Tags[] | select(.Key == "Name1")'
<エラー内容>
jq: error: Name1/0 is not defined at <top-level>, line 1: .Reservations[].Instances[] |.InstanceId, .Tags[] | select(.Key == Name1) jq: 1 compile error
想定している結果
想定している結果は以下の通りです。i-***********2のTags[]のKeyにName1はないためValueは出力しないようにしたいです。
i-***********1,test1
i-***********2,
i-***********3,test1
回答1件
あなたの回答
tips
プレビュー