我正在尝试使用以下查询提取有关我的某些 ELB 实例的一些相关信息
aws elb describe-load-balancers --load-balancer-name my-elb \
--query 'LoadBalancerDescriptions[*].[LoadBalancerName, DNSName, Scheme ,\
SourceSecurityGroup.GroupName , SecurityGroups, Instances[*].InstanceId ]
输出如下
- - - my-elb
- my-elb-15451562.us-east-1.elb.amazonaws.com
- internet-facing
- my-elb-sg
- - sg-ef113b0d
- - i-00633c0d3a103b3e9
我的问题是:当我执行 --output text
时,最后两个元素各自以单独的行结束。这会破坏 TSV 格式和随后的 LibreCalc 输入。
如何防止这种情况发生,以便 TSV 的导入显示在一行上?
答案 0 :(得分:2)
问题在于 ELB 和实例之间存在一对多关系。一个 ELB 可以链接多个实例。这使得难以显示为 CSV/TSV,因为列数是可变的。
如果您知道有一个固定数量的实例总是与该 ELB 相关联,那么您可以将查询更改为:
aws elb describe-load-balancers --load-balancer-name my-elb \
--query 'LoadBalancerDescriptions[*].[LoadBalancerName, DNSName, Scheme ,\
SourceSecurityGroup.GroupName , SecurityGroups, Instances[0].InstanceId, Instances[1].InstanceId ]
答案 1 :(得分:1)
通过管道传递粘贴命令 | paste -d"\t" -s
当有未知的新行时,我想知道有 SED 或 AWK 解决方案吗?
答案 2 :(得分:1)
您实际上可以通过创建一个人工数组来解决这个问题,然后在这个人工数组上使用 flatten operator — []
—。
给定 JSON:
{
"LoadBalancerDescriptions": [
{
"Subnets": [
"subnet-15aaab61"
],
"CanonicalHostedZoneNameID": "Z3DZXE0EXAMPLE",
"CanonicalHostedZoneName": "my-load-balancer-1234567890.us-west-2.elb.amazonaws.com",
"ListenerDescriptions": [
{
"Listener": {
"InstancePort": 80,
"LoadBalancerPort": 80,
"Protocol": "HTTP",
"InstanceProtocol": "HTTP"
},
"PolicyNames": []
},
{
"Listener": {
"InstancePort": 443,
"SSLCertificateId": "arn:aws:iam::123456789012:server-certificate/my-server-cert",
"LoadBalancerPort": 443,
"Protocol": "HTTPS",
"InstanceProtocol": "HTTPS"
},
"PolicyNames": [
"ELBSecurityPolicy-2015-03"
]
}
],
"HealthCheck": {
"HealthyThreshold": 2,
"Interval": 30,
"Target": "HTTP:80/png",
"Timeout": 3,
"UnhealthyThreshold": 2
},
"VPCId": "vpc-a01106c2",
"BackendServerDescriptions": [
{
"InstancePort": 80,
"PolicyNames": [
"my-ProxyProtocol-policy"
]
}
],
"Instances": [
{
"InstanceId": "i-207d9717"
},
{
"InstanceId": "i-afefb49b"
}
],
"DNSName": "my-load-balancer-1234567890.us-west-2.elb.amazonaws.com",
"SecurityGroups": [
"sg-a61988c3"
],
"Policies": {
"LBCookieStickinessPolicies": [
{
"PolicyName": "my-duration-cookie-policy",
"CookieExpirationPeriod": 60
}
],
"AppCookieStickinessPolicies": [],
"OtherPolicies": [
"my-PublicKey-policy",
"my-authentication-policy",
"my-SSLNegotiation-policy",
"my-ProxyProtocol-policy",
"ELBSecurityPolicy-2015-03"
]
},
"LoadBalancerName": "my-load-balancer",
"CreatedTime": "2015-03-19T03:24:02.650Z",
"AvailabilityZones": [
"us-west-2a"
],
"Scheme": "internet-facing",
"SourceSecurityGroup": {
"OwnerAlias": "123456789012",
"GroupName": "my-elb-sg"
}
}
]
}
以https://docs.aws.amazon.com/cli/latest/reference/elb/describe-load-balancers.html#examples
为例查询:
LoadBalancerDescriptions[*].[ [LoadBalancerName, DNSName, Scheme, SourceSecurityGroup.GroupName, SecurityGroups, Instances[*].InstanceId][] ]
将产生 JSON
[
[
[
"my-load-balancer",
"my-load-balancer-1234567890.us-west-2.elb.amazonaws.com",
"internet-facing",
"my-elb-sg",
"sg-a61988c3",
"i-207d9717",
"i-afefb49b"
]
]
]
相当于 YAML
- - - my-load-balancer
- my-load-balancer-1234567890.us-west-2.elb.amazonaws.com
- internet-facing
- my-elb-sg
- sg-a61988c3
- i-207d9717
- i-afefb49b
根据需要,您可以根据需要进一步展平:
LoadBalancerDescriptions[*].[ [LoadBalancerName, DNSName, Scheme, SourceSecurityGroup.GroupName, SecurityGroups, Instances[*].InstanceId][] ][]
给予
- - my-load-balancer
- my-load-balancer-1234567890.us-west-2.elb.amazonaws.com
- internet-facing
- my-elb-sg
- sg-a61988c3
- i-207d9717
- i-afefb49b
和
LoadBalancerDescriptions[*].[ [LoadBalancerName, DNSName, Scheme, SourceSecurityGroup.GroupName, SecurityGroups, Instances[*].InstanceId][] ][][]
给予
- my-load-balancer
- my-load-balancer-1234567890.us-west-2.elb.amazonaws.com
- internet-facing
- my-elb-sg
- sg-a61988c3
- i-207d9717
- i-afefb49b