作为我正在编写的Shell脚本的一部分,我正在查询AWS(cli)以提取有关可用安全组名称和ID的信息,如下所示:
aws ec2 describe-security-groups | jq -r '.SecurityGroups[]'
{
"IpPermissionsEgress": [
{
"IpProtocol": "-1",
"PrefixListIds": [],
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"UserIdGroupPairs": [],
"Ipv6Ranges": []
}
],
"Description": "default VPC security group",
"IpPermissions": [
{
"PrefixListIds": [],
"FromPort": 80,
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"ToPort": 80,
"IpProtocol": "tcp",
"UserIdGroupPairs": [],
"Ipv6Ranges": [
{
"CidrIpv6": "::/0"
}
]
},
{
"PrefixListIds": [],
"FromPort": 22,
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"ToPort": 22,
"IpProtocol": "tcp",
"UserIdGroupPairs": [],
"Ipv6Ranges": []
},
{
"PrefixListIds": [],
"FromPort": -1,
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"ToPort": -1,
"IpProtocol": "icmp",
"UserIdGroupPairs": [],
"Ipv6Ranges": []
}
],
"GroupName": "default",
"VpcId": "vpc-b3c29bcb",
"OwnerId": "506490286752",
"GroupId": "sg-83db2ef7"
}
并使用jq
,我试图返回显示如下信息的地图列表:
GroupName,GroupId
这是我尝试过的:
aws ec2 describe-security-groups | jq -r '.SecurityGroups[] | to_entries[] | [ .GroupName.value , .GroupId.value]'
使用上述方法将返回:
[
null,
null
]
[
null,
null
]
在示例中,定界符为“,”,但我希望显示的输出像这样(示例):
"default - sg-abd837s"
如何使用jq
来完成?
答案 0 :(得分:2)
使用字符串插值:
.SecurityGroups[] | "\(.GroupName) - \(.GroupId)"
答案 1 :(得分:1)
不需要使用to_entries
:
.SecurityGroups[]
| [ .GroupName, .GroupId ]
| join(" - ")
产生:
“默认-sg-abd837s”