将JSON解析为jq以从AWS CLI获取列表

时间:2017-10-19 14:51:26

标签: aws-cli jq

我想获得一个任务定义列表,我可以通过AWS CLI创建列表。以下是命令的部分列表;

aws ecs list-task-definitions --region eu-west-1

{
    "taskDefinitionArns": [
        "arn:aws:ecs:eu-west-1:543573289192:task-definition/audience-view-dev-api:4", 
        "arn:aws:ecs:eu-west-1:543573289192:task-definition/audience-view-dev-groups:4", 
        "arn:aws:ecs:eu-west-1:543573289192:task-definition/audience-view-dev-groups:5", 
        "arn:aws:ecs:eu-west-1:543573289192:task-definition/audience-view-dev-mock-api:1", 
        "arn:aws:ecs:eu-west-1:543573289192:task-definition/audience-view-dev-mock-responder:1"
    ]
}

从此我想只展示;

audience-view-dev-api
audience-view-dev-groups
audience-view-dev-groups
audience-view-dev-mock-api
audience-view-dev-mock-responder

我相信如果我将其传达给jq那么我可以得到我之后的所有内容?我正在尝试各种组合; aws ecs list-task-definitions --region eu-west-1 | jq -r '.taskDefinitionArns[].<something_here>'无济于事

3 个答案:

答案 0 :(得分:4)

试试这个:

$ aws ecs list-task-definitions --region eu-west-1 \
  | jq -Mr '.taskDefinitionArns[] | split(":")[5] | split("/")[1]'

展开:

  .taskDefinitionArns[]     # for each taskDefinitionArns
| split(":")[5]             # take entry [5] after splitting by :
| split("/")[1]             # take entry [1] after splitting by /

答案 1 :(得分:3)

使用 jq match 功能:

aws ecs list-task-definitions --region eu-west-1 \
| jq -r '.taskDefinitionArns[] | match(".*/([^:]+):[0-9]+$").captures[0].string'
  • ".*/([^:]+):[0-9]+$" - 与/和最后:个字符之间的序列匹配的正则表达式模式

输出:

audience-view-dev-api
audience-view-dev-groups
audience-view-dev-groups
audience-view-dev-mock-api
audience-view-dev-mock-responder

https://stedolan.github.io/jq/manual/v1.5/#match(val),match(regex;flags)

答案 2 :(得分:1)

要获取唯一值:

aws ecs list-task-definitions | jq -r '.taskDefinitionArns[] | match(".*/([^:]+):[0-9]+$").captures[0].string' | sort | uniq