質問編集履歴

2

jsonファイルの内容を修正しました。また想定している結果を追加しました。

2020/02/10 02:14

投稿

denden_tom
denden_tom

スコア5

test CHANGED
File without changes
test CHANGED
@@ -1,32 +1,18 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
-
4
-
5
- あらかじめ取得した以下のコマンドの結果をjsonファイルで保存しています
3
+ あらかじめ以下のコマンドの結果をjsonファイルで保存しています
6
4
 
7
5
  # aws ec2 describe-instances
8
6
 
9
7
 
10
8
 
11
- 結果のjsonは以下のような感じになっているとします。(見やすさのため一部省略させていただきました)
12
-
13
- このときjqを使ってInstanceとTags[]のKeyの値がName1のValueの値を取得したいのですが、どのようなコマンドをワンライナーで書けますでしょうか?
9
+ 結果のjsonは以下のようになっているとします。この時、jqを使ってInstanceIdとTags[]のKeyの値がName1のValueの値を取得したいのですが、どのようなコマンドをワンライナーで書けますでしょうか?
14
-
15
-
16
-
10
+
17
- InstanceによってはTags[]のKeyにName1がないものもあります。
11
+ InstanceによってはTags[]のKeyにName1がないものもあるため、そのValueはnull(何も出力しない)としたいです。
18
-
19
- Selectを使えば、Key=="Name1"があるInstance情報を取り出せると思うのですが、
12
+
20
-
21
- こちらはKey=="Name1"がないInstanceのValueはnullとして出したいです。
13
+
22
-
23
-
24
-
25
- この場合のコマンドがわからず、投稿させていただきました。
14
+
26
-
27
-
28
-
29
- ```ここに言語を入力
15
+ ```JSON
30
16
 
31
17
  {
32
18
 
@@ -44,15 +30,81 @@
44
30
 
45
31
  {
46
32
 
33
+ "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal",
34
+
35
+ "EbsOptimized": false,
36
+
37
+ "Monitoring": {
38
+
39
+ "State": "disabled"
40
+
41
+ },
42
+
43
+ "State": {
44
+
45
+ "Code": 80
46
+
47
+ },
48
+
49
+ "NetworkInterfaces": [
50
+
51
+ {
52
+
53
+ "PrivateDnsName": "ip-********.ap-northeast-1.compute.internal",
54
+
55
+ "PrivateIpAddresses": [
56
+
57
+ {
58
+
59
+ "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal",
60
+
61
+ "PrivateIpAddress": "172.**.**.***",
62
+
63
+ "Primary": true
64
+
65
+ }
66
+
67
+ ],
68
+
69
+ "OwnerId": "************",
70
+
71
+ "Groups": [
72
+
73
+ {
74
+
75
+ "GroupName": "RDP",
76
+
77
+ "GroupId": "sg-********"
78
+
79
+ }
80
+
81
+ ],
82
+
83
+ "PrivateIpAddress": "172.**.**.***",
84
+
85
+ "Description": "",
86
+
87
+ "SourceDestCheck": true,
88
+
89
+ "MacAddress": "************",
90
+
91
+ "Status": "in-use",
92
+
93
+ "NetworkInterfaceId": "eni-************",
94
+
95
+ "VpcId": "vpc-************",
96
+
97
+ "SubnetId": "subnet-************"
98
+
99
+ }
100
+
101
+ ],
102
+
47
103
  "PrivateIpAddress": "172.**.**.***",
48
104
 
49
- "Hypervisor": "xen",
105
+ "RootDeviceType": "ebs",
50
-
51
- "KeyName": "macpro",
106
+
52
-
53
- "RootDeviceName": "/dev/sda1",
107
+ "StateTransitionReason": "User initiated (2016-02-24 10:01:43 GMT)",
54
-
55
- "ImageId": "ami-************",
56
108
 
57
109
  "LaunchTime": "2016-02-24T05:53:59.000Z",
58
110
 
@@ -86,29 +138,107 @@
86
138
 
87
139
  "SubnetId": "subnet-************",
88
140
 
89
- "Architecture": "x86_64",
90
-
91
- "InstanceType": "t2.micro",
92
-
93
141
  "Platform": "windows",
94
142
 
143
+ "StateReason": {
144
+
145
+ "Message": "Client.UserInitiatedShutdown: User initiated shutdown",
146
+
147
+ "Code": "Client.UserInitiatedShutdown"
148
+
149
+ },
150
+
95
- "InstanceId": "i-************",
151
+ "InstanceId": "i-***********1",
152
+
153
+ "SourceDestCheck": true,
154
+
155
+ "ClientToken": "rduVq************",
156
+
157
+ "AmiLaunchIndex": 0
96
158
 
97
159
  }
98
160
 
161
+ ]
162
+
99
163
  "Instances": [
100
164
 
101
165
  {
102
166
 
167
+ "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal",
168
+
169
+ "EbsOptimized": false,
170
+
171
+ "Monitoring": {
172
+
173
+ "State": "disabled"
174
+
175
+ },
176
+
177
+ "State": {
178
+
179
+ "Code": 80
180
+
181
+ },
182
+
183
+ "NetworkInterfaces": [
184
+
185
+ {
186
+
187
+ "PrivateDnsName": "ip-********.ap-northeast-1.compute.internal",
188
+
189
+ "PrivateIpAddresses": [
190
+
191
+ {
192
+
193
+ "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal",
194
+
195
+ "PrivateIpAddress": "172.**.**.***",
196
+
197
+ "Primary": true
198
+
199
+ }
200
+
201
+ ],
202
+
203
+ "OwnerId": "************",
204
+
205
+ "Groups": [
206
+
207
+ {
208
+
209
+ "GroupName": "RDP",
210
+
211
+ "GroupId": "sg-********"
212
+
213
+ }
214
+
215
+ ],
216
+
217
+ "PrivateIpAddress": "172.**.**.***",
218
+
219
+ "Description": "",
220
+
221
+ "SourceDestCheck": true,
222
+
223
+ "MacAddress": "************",
224
+
225
+ "Status": "in-use",
226
+
227
+ "NetworkInterfaceId": "eni-************",
228
+
229
+ "VpcId": "vpc-************",
230
+
231
+ "SubnetId": "subnet-************"
232
+
233
+ }
234
+
235
+ ],
236
+
103
237
  "PrivateIpAddress": "172.**.**.***",
104
238
 
105
- "Hypervisor": "xen",
239
+ "RootDeviceType": "ebs",
106
-
107
- "KeyName": "macpro",
240
+
108
-
109
- "RootDeviceName": "/dev/sda1",
241
+ "StateTransitionReason": "User initiated (2016-02-24 10:01:43 GMT)",
110
-
111
- "ImageId": "ami-************",
112
242
 
113
243
  "LaunchTime": "2016-02-24T05:53:59.000Z",
114
244
 
@@ -134,34 +264,108 @@
134
264
 
135
265
  "SubnetId": "subnet-************",
136
266
 
137
- "Architecture": "x86_64",
138
-
139
- "InstanceType": "t2.micro",
140
-
141
267
  "Platform": "windows",
142
268
 
269
+ "StateReason": {
270
+
271
+ "Message": "Client.UserInitiatedShutdown: User initiated shutdown",
272
+
273
+ "Code": "Client.UserInitiatedShutdown"
274
+
275
+ },
276
+
143
- "InstanceId": "i-************",
277
+ "InstanceId": "i-***********2",
278
+
279
+ "SourceDestCheck": true,
280
+
281
+ "ClientToken": "rduVq************",
282
+
283
+ "AmiLaunchIndex": 0
144
284
 
145
285
  }
146
286
 
287
+ ]
288
+
147
289
  "Instances": [
148
290
 
149
291
  {
150
292
 
293
+ "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal",
294
+
295
+ "EbsOptimized": false,
296
+
297
+ "Monitoring": {
298
+
299
+ "State": "disabled"
300
+
301
+ },
302
+
303
+ "State": {
304
+
305
+ "Code": 80
306
+
307
+ },
308
+
309
+ "NetworkInterfaces": [
310
+
311
+ {
312
+
313
+ "PrivateDnsName": "ip-********.ap-northeast-1.compute.internal",
314
+
315
+ "PrivateIpAddresses": [
316
+
317
+ {
318
+
319
+ "PrivateDnsName": "ip-****.ap-northeast-1.compute.internal",
320
+
321
+ "PrivateIpAddress": "172.**.**.***",
322
+
323
+ "Primary": true
324
+
325
+ }
326
+
327
+ ],
328
+
329
+ "OwnerId": "************",
330
+
331
+ "Groups": [
332
+
333
+ {
334
+
335
+ "GroupName": "RDP",
336
+
337
+ "GroupId": "sg-********"
338
+
339
+ }
340
+
341
+ ],
342
+
343
+ "PrivateIpAddress": "172.**.**.***",
344
+
345
+ "Description": "",
346
+
347
+ "SourceDestCheck": true,
348
+
349
+ "MacAddress": "************",
350
+
351
+ "Status": "in-use",
352
+
353
+ "NetworkInterfaceId": "eni-************",
354
+
355
+ "VpcId": "vpc-************",
356
+
357
+ "SubnetId": "subnet-************"
358
+
359
+ }
360
+
361
+ ],
362
+
151
363
  "PrivateIpAddress": "172.**.**.***",
152
364
 
153
365
  "RootDeviceType": "ebs",
154
366
 
155
367
  "StateTransitionReason": "User initiated (2016-02-24 10:01:43 GMT)",
156
368
 
157
- "Hypervisor": "xen",
158
-
159
- "KeyName": "macpro",
160
-
161
- "RootDeviceName": "/dev/sda1",
162
-
163
- "ImageId": "ami-************",
164
-
165
369
  "LaunchTime": "2016-02-24T05:53:59.000Z",
166
370
 
167
371
  "Tags": [
@@ -194,13 +398,23 @@
194
398
 
195
399
  "SubnetId": "subnet-************",
196
400
 
197
- "Architecture": "x86_64",
198
-
199
- "InstanceType": "t2.micro",
200
-
201
401
  "Platform": "windows",
202
402
 
403
+ "StateReason": {
404
+
405
+ "Message": "Client.UserInitiatedShutdown: User initiated shutdown",
406
+
407
+ "Code": "Client.UserInitiatedShutdown"
408
+
409
+ },
410
+
203
- "InstanceId": "i-************",
411
+ "InstanceId": "i-***********3",
412
+
413
+ "SourceDestCheck": true,
414
+
415
+ "ClientToken": "rduVq************",
416
+
417
+ "AmiLaunchIndex": 0
204
418
 
205
419
  }
206
420
 
@@ -218,21 +432,23 @@
218
432
 
219
433
  ### 発生している問題・エラーメッセージ
220
434
 
221
-
222
-
223
- 事前に取得したjsonファイルのファイル名をxxx.jsonとして、
435
+ 事前に取得したjsonファイルのファイル名をxxx.jsonとして、以下のコマンドを試しましたが、エラーが発生します。
436
+
224
-
437
+ 当方の環境はpowershell6でございます。
438
+
439
+
440
+
225
- Get-Content xxx.json | jq -r '.Reservations[].Instances[] | .Tags[] | select(.Key == Service)'
441
+ Get-Content xxx.json | jq -r '.Reservations[].Instances[] |.InstanceId, .Tags[] | select(.Key == "Name1")'
226
-
227
-
228
-
442
+
443
+
444
+
229
- など試しましたが、以下のようなエラーがあり、なかなか問題解決しません。
445
+ エラー内容>
230
446
 
231
447
  ```
232
448
 
233
- jq: error: Service/0 is not defined at <top-level>, line 1:
449
+ jq: error: Name1/0 is not defined at <top-level>, line 1:
234
-
450
+
235
- .Reservations[].Instances[] | .Tags[] | select(.Key == Service)
451
+ .Reservations[].Instances[] |.InstanceId, .Tags[] | select(.Key == Name1)
236
452
 
237
453
  jq: 1 compile error
238
454
 
@@ -240,4 +456,12 @@
240
456
 
241
457
 
242
458
 
459
+ ### 想定している結果
460
+
461
+ 想定している結果は以下の通りです。i-***********2のTags[]のKeyにName1はないためValueは出力しないようにしたいです。
462
+
243
- 当方の環境はpowershell6でございます。
463
+ i-***********1,test1
464
+
465
+ i-***********2,
466
+
467
+ i-***********3,test1

1

タグの追加

2020/02/10 02:14

投稿

denden_tom
denden_tom

スコア5

test CHANGED
File without changes
test CHANGED
File without changes