
저는 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 스크립트에 의해 처리될 예정이라면 왜 따옴표가 필요한지 잘 모르겠습니다. 그러나 이렇게 하면 sed 표현식의 대체 부분에 \1
and를 따옴표로 묶으면 됩니다.\2
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"}
...등
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"