jq:如何从输出中替换 "null"?

jq: How to substitute "null" from the output?

我有以下 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

我想从输出中删除空值,该怎么做?

.FromPort 键添加一个 fallback

.FromPort // ""

所以命令变成了;

.SecurityGroups[].IpPermissions[] | [ (.FromPort // "" ?), .IpProtocol, .IpRanges[].CidrIp ] | @tsv

哪个屈服于

    -1  0.0.0.0/0
22  tcp 0.0.0.0/0

Try it online!