#やりたいこと
下記JSONを下記のようなcsv形式に変換をしたい。
※1行目のヘッダはなくてもよい
csv
1Description,FromPort,ToPort,IpProtocol,CidrIp 2sg1,,,-1,"172.26.1.0/24" 3sg1,22,22,"tcp","192.168.0.1/32" 4sg1,22,22,"tcp","192.168.0.2/32" 5sg1,22,22,"tcp","192.168.0.3/32" 6sg1,244,255,"tcp","192.168.0.1/32" 7sg2,0,65535,"tcp","172.26.1.0/24"
json
1{ 2 "SecurityGroups": [ 3 { 4 "IpPermissionsEgress": [ 5 { 6 "IpProtocol": "-1", 7 "PrefixListIds": [], 8 "IpRanges": [ 9 { 10 "CidrIp": "0.0.0.0/0" 11 } 12 ], 13 "UserIdGroupPairs": [], 14 "Ipv6Ranges": [] 15 } 16 ], 17 "Description": "sg2", 18 "IpPermissions": [ 19 { 20 "PrefixListIds": [], 21 "FromPort": 0, 22 "IpRanges": [ 23 { 24 "CidrIp": "172.26.1.0/24" 25 } 26 ], 27 "ToPort": 65535, 28 "IpProtocol": "tcp", 29 "UserIdGroupPairs": [], 30 "Ipv6Ranges": [] 31 } 32 ], 33 "GroupName": "sg2", 34 "VpcId": "vpc-0b749cd08a045897f", 35 "OwnerId": "273384484291", 36 "GroupId": "sg-0a14097162ef9b499" 37 }, 38 { 39 "IpPermissionsEgress": [ 40 { 41 "IpProtocol": "-1", 42 "PrefixListIds": [], 43 "IpRanges": [ 44 { 45 "CidrIp": "0.0.0.0/0" 46 } 47 ], 48 "UserIdGroupPairs": [], 49 "Ipv6Ranges": [] 50 } 51 ], 52 "Description": "sg1", 53 "IpPermissions": [ 54 { 55 "IpProtocol": "-1", 56 "PrefixListIds": [], 57 "IpRanges": [ 58 { 59 "CidrIp": "172.26.1.0/24" 60 } 61 ], 62 "UserIdGroupPairs": [], 63 "Ipv6Ranges": [] 64 }, 65 { 66 "PrefixListIds": [], 67 "FromPort": 22, 68 "IpRanges": [ 69 { 70 "CidrIp": "192.168.0.1/32" 71 }, 72 { 73 "CidrIp": "192.168.0.2/32" 74 }, 75 { 76 "CidrIp": "192.168.0.3/32" 77 } 78 ], 79 "ToPort": 22, 80 "IpProtocol": "tcp", 81 "UserIdGroupPairs": [], 82 "Ipv6Ranges": [] 83 }, 84 { 85 "PrefixListIds": [], 86 "FromPort": 244, 87 "IpRanges": [ 88 { 89 "CidrIp": "192.168.0.1/32" 90 } 91 ], 92 "ToPort": 255, 93 "IpProtocol": "tcp", 94 "UserIdGroupPairs": [], 95 "Ipv6Ranges": [] 96 } 97 ], 98 "GroupName": "sg1", 99 "VpcId": "vpc-0b749cd08a045897f", 100 "OwnerId": "273384484291", 101 "GroupId": "sg-0c1cf5220455208ce" 102 } 103 ] 104}
#今のところ一番近く出来たコマンド
cli
1[root@ip-192-168-0-65 ec2-user]# cat describe-security-groups | jq -r -c '.SecurityGroups[]|.GroupName,(.IpPermissions[] | [.FromPort,.ToPort,.IpProtocol,.IpRanges[].CidrIp] | @csv)' 2sg2 30,65535,"tcp","172.26.1.0/24" 4sg1 5,,"-1","172.26.1.0/24" 622,22,"tcp","192.168.0.1/32","192.168.0.2/32","192.168.0.3/32" 7244,255,"tcp","192.168.0.1/32" 8[root@ip-192-168-0-65 ec2-user]#
#jqコマンドで悩んでいるところ
DescriptionとIpPermissions[].FromPortのように階層が違うものを横に並べることが上手く出来ていない
jqマスターさん助けて!!
あなたの回答
tips
プレビュー