我是 jq 的新手,并尝试从选定的字段中提取键和值。
{
"StackEvents": [
{
"StackId": "arn:aws:cloudformation:us-east-1:1234567890123:stack/jenkinscf-s3/f64657b0-cb0a-11ed-848f-0a093f43c24f",
"EventId": "06263740-cb0b-11ed-86ee-0e3b4b6ffec3",
"StackName": "jenkinscf-s3",
"LogicalResourceId": "jenkinscf-s3",
"PhysicalResourceId": "arn:aws:cloudformation:us-east-1:1234567890123:stack/jenkinscf-s3/f64657b0-cb0a-11ed-848f-0a093f43c24f",
"ResourceType": "AWS::CloudFormation::Stack",
"Timestamp": "2023-03-25T12:46:12.647000+00:00",
"ResourceStatus": "CREATE_COMPLETE"
},
{
"StackId": "arn:aws:cloudformation:us-east-1:1234567890123:stack/jenkinscf-s3/f64657b0-cb0a-11ed-848f-0a093f43c24f",
"EventId": "S3Bucket-CREATE_COMPLETE-2023-03-25T12:46:11.127Z",
"StackName": "jenkinscf-s3",
"LogicalResourceId": "S3Bucket",
"PhysicalResourceId": "jenkinscf-s3-s3bucket-tmh6orxmeegs",
"ResourceType": "AWS::S3::Bucket",
"Timestamp": "2023-03-25T12:46:11.127000+00:00",
"ResourceStatus": "CREATE_COMPLETE",
"ResourceProperties": "{}"
},
{
"StackId": "arn:aws:cloudformation:us-east-1:1234567890123:stack/jenkinscf-s3/f64657b0-cb0a-11ed-848f-0a093f43c24f",
"EventId": "S3Bucket-CREATE_IN_PROGRESS-2023-03-25T12:45:50.261Z",
"StackName": "jenkinscf-s3",
"LogicalResourceId": "S3Bucket",
"PhysicalResourceId": "jenkinscf-s3-s3bucket-tmh6orxmeegs",
"ResourceType": "AWS::S3::Bucket",
"Timestamp": "2023-03-25T12:45:50.261000+00:00",
"ResourceStatus": "CREATE_IN_PROGRESS",
"ResourceStatusReason": "Resource creation Initiated",
"ResourceProperties": "{}"
},
{
"StackId": "arn:aws:cloudformation:us-east-1:1234567890123:stack/jenkinscf-s3/f64657b0-cb0a-11ed-848f-0a093f43c24f",
"EventId": "S3Bucket-CREATE_IN_PROGRESS-2023-03-25T12:45:49.665Z",
"StackName": "jenkinscf-s3",
"LogicalResourceId": "S3Bucket",
"PhysicalResourceId": "",
"ResourceType": "AWS::S3::Bucket",
"Timestamp": "2023-03-25T12:45:49.665000+00:00",
"ResourceStatus": "CREATE_IN_PROGRESS",
"ResourceProperties": "{}"
},
{
"StackId": "arn:aws:cloudformation:us-east-1:1234567890123:stack/jenkinscf-s3/f64657b0-cb0a-11ed-848f-0a093f43c24f",
"EventId": "f64b87d0-cb0a-11ed-848f-0a093f43c24f",
"StackName": "jenkinscf-s3",
"LogicalResourceId": "jenkinscf-s3",
"PhysicalResourceId": "arn:aws:cloudformation:us-east-1:1234567890123:stack/jenkinscf-s3/f64657b0-cb0a-11ed-848f-0a093f43c24f",
"ResourceType": "AWS::CloudFormation::Stack",
"Timestamp": "2023-03-25T12:45:46.136000+00:00",
"ResourceStatus": "CREATE_IN_PROGRESS",
"ResourceStatusReason": "User Initiated"
}
]
}
预期产出
"LogicalResourceId": "jenkinscf-s3"
"ResourceStatus": "CREATE_COMPLETE"
我尝试过的解决方案只能获得键或值,但不能同时获得两者。
.StackEvents | .[] | .LogicalResourceId, .ResourceStatus
"jenkinscf-s3"
"CREATE_COMPLETE"
"S3Bucket"
"CREATE_COMPLETE"
"S3Bucket"
"CREATE_IN_PROGRESS"
"S3Bucket"
"CREATE_IN_PROGRESS"
"jenkinscf-s3"
"CREATE_IN_PROGRESS"
.StackEvents[] | keys
[
"EventId",
"LogicalResourceId",
"PhysicalResourceId",
"ResourceStatus",
"ResourceType",
"StackId",
"StackName",
"Timestamp"
]
我的最终目标是在 bash 脚本中使用它。该脚本将检测堆栈中的漂移并输出差异。它当前运行并且仅输出值,因此任何不熟悉该脚本的人只要查看键本身就会很容易感到困惑。
答案1
一个快速的 sed 解决方案:
$ sed -n 's/^ *"\(LogicalResourceId\|ResourceStatus\)": "\([^"]*\)",*/\1: \2/p' input.json
LogicalResourceId: jenkinscf-s3
ResourceStatus: CREATE_COMPLETE
LogicalResourceId: S3Bucket
ResourceStatus: CREATE_COMPLETE
LogicalResourceId: S3Bucket
ResourceStatus: CREATE_IN_PROGRESS
LogicalResourceId: S3Bucket
ResourceStatus: CREATE_IN_PROGRESS
LogicalResourceId: jenkinscf-s3
ResourceStatus: CREATE_IN_PROGRESS
与您想要的输出不同,没有引号,但我不确定如果此输出打算稍后由 bash 脚本处理,您为什么需要它们。不过,如果您这样做,只需将\1
和\2
放在 sed 表达式的替换部分中的引号中即可。
这样做jq
也很容易:
$ jq -r '.StackEvents[] | "LogicalResourceId: \(.LogicalResourceId)", "ResourceStatus: \(.ResourceStatus)"' input.json
LogicalResourceId: jenkinscf-s3
ResourceStatus: CREATE_COMPLETE
LogicalResourceId: S3Bucket
ResourceStatus: CREATE_COMPLETE
LogicalResourceId: S3Bucket
ResourceStatus: CREATE_IN_PROGRESS
LogicalResourceId: S3Bucket
ResourceStatus: CREATE_IN_PROGRESS
LogicalResourceId: jenkinscf-s3
ResourceStatus: CREATE_IN_PROGRESS
如果您需要的话,在此处添加引号也很容易,但您必须使用反斜杠将其转义。
答案2
您可以jq
像awk
这样使用:
#!/bin/bash
input=$(cat input.json)
output=$(echo "$input" | jq -r '.StackEvents[] | {LogicalResourceId, ResourceStatus}')
echo "$output" | awk '{gsub(/[{}]/,"")}NF'
jq 输出:
{LogicalResourceId": "jenkinscf-s3",
ResourceStatus": "CREATE_COMPLETE"}
{"LogicalResourceId": "S3Bucket",
"ResourceStatus": "CREATE_COMPLETE"}
... ETC
awk 删除{}
并空行。
最终的输出是:
"LogicalResourceId": "jenkinscf-s3",
"ResourceStatus": "CREATE_COMPLETE"
"LogicalResourceId": "S3Bucket",
"ResourceStatus": "CREATE_COMPLETE"
"LogicalResourceId": "S3Bucket",
"ResourceStatus": "CREATE_IN_PROGRESS"
"LogicalResourceId": "S3Bucket",
"ResourceStatus": "CREATE_IN_PROGRESS"
"LogicalResourceId": "jenkinscf-s3",
"ResourceStatus": "CREATE_IN_PROGRESS"