我有以下JSON:
{
"SecurityGroups": [
{
"Description": "launch-wizard-6 created 2018-10-25T13:59:20.092+03:00",
"GroupName": "launch-wizard-6",
"IpPermissions": [
{
"IpProtocol": "-1",
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"Ipv6Ranges": [],
"PrefixListIds": [],
"UserIdGroupPairs": []
},
{
"FromPort": 22,
"IpProtocol": "tcp",
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"Ipv6Ranges": [],
"PrefixListIds": [],
"ToPort": 22,
"UserIdGroupPairs": []
}
],
"OwnerId": "XXXXXXXXXXXX",
"GroupId": "sg-054368f50a6b4fea4",
"IpPermissionsEgress": [
{
"IpProtocol": "-1",
"IpRanges": [
{
"CidrIp": "0.0.0.0/0"
}
],
"Ipv6Ranges": [
{
"CidrIpv6": "::/0"
}
],
"PrefixListIds": [],
"UserIdGroupPairs": []
}
],
"VpcId": "vpc-6ea82a08"
}
]
}
当我运行以下jq命令时:
aws ec2 describe-security-groups --group-id ${group_id} --profile ${profile} --region ${region} --output json | jq -r '.SecurityGroups[].IpPermissions[] | [ (.FromPort|tostring), .IpProtocol, .IpRanges[].CidrIp // .UserIdGroupPairs[].GroupId // "" ] | @tsv'
我得到以下输出:
null -1 0.0.0.0/0
22 tcp 0.0.0.0/0
我想从输出中删除null,怎么做?
为.FromPort
密钥添加回退:
.FromPort // ""
因此,命令变为;
.SecurityGroups[].IpPermissions[] | [ (.FromPort // "" ?), .IpProtocol, .IpRanges[].CidrIp ] | @tsv
哪一个屈服于
-1 0.0.0.0/0
22 tcp 0.0.0.0/0
在线试用!