여러 패턴을 파악하고 첫 번째 패턴을 삭제하는 방법

여러 패턴을 파악하고 첫 번째 패턴을 삭제하는 방법

내가 시도한 코드는 내가 원하는 정확한 결과를 얻지 못했습니다. 이것은 내가 시도한 코드입니다.

   curl -s --request GET \
    http://10.10.5.242/api/v1/incidents \
    -H "Content-Type: application/json;" \
    -H "X-Cachet-Token: ROvbssneyYwR8fwNgOWj" \
     | json_pp | grep -e id -e component_id

이것은 이것을 출력합니다

 "component_id" : "4",
 "id" : 1,
 "id" : 2,
 "component_id" : "4",
 "id" : 3,
 "component_id" : "4",
 "component_id" : "4",
 "id" : 4
 "component_id" : "3",
 "id" : 5,
 "component_id" : "2",
 "id" : 6,

제가 사용하고 있는 API의 내용입니다.http://10.10.5.242/api/v1/incidents

{
        "meta": {
            "pagination": {
                "total": 6,
                "count": 6,
                "per_page": 20,
                "current_page": 1,
                "total_pages": 1,
                "links": {
                    "next_page": null,
                    "previous_page": null
                }
            }
        },
        "data": [
            {
                "id": 1,
                "component_id": "4",
                "name": "Service Unavailable",
                "status": "4",
                "visible": 1,
                "message": "Server is not responding",
                "scheduled_at": "2018-02-26 10:05:03",
                "created_at": "2018-02-23 14:56:16",
                "updated_at": "2018-02-26 08:37:11",
                "deleted_at": null,
                "human_status": "Fixed"
            },
            {
                "id": 2,
                "component_id": "4",
                "name": "Service Unavailable",
                "status": "4",
                "visible": 1,
                "message": "Server is not responding",
                "scheduled_at": "2018-02-26 10:05:03",
                "created_at": "2018-02-23 15:39:52",
                "updated_at": "2018-02-26 08:37:11",
                "deleted_at": null,
                "human_status": "Fixed"
            },
            {
                "id": 3,
                "component_id": "4",
                "name": "Service Unavailable",
                "status": "4",
                "visible": 1,
                "message": "Server is not responding",
                "scheduled_at": "2018-02-26 10:05:03",
                "created_at": "2018-02-26 08:15:43",
                "updated_at": "2018-02-26 08:37:12",
                "deleted_at": null,
                "human_status": "Fixed"
            },
            {
                "id": 4,
                "component_id": "4",
                "name": "Service Unavailable",
                "status": "4",
                "visible": 1,
                "message": "Server is not responding",
                "scheduled_at": "2018-02-26 10:05:03",
                "created_at": "2018-02-26 08:19:12",
                "updated_at": "2018-02-26 08:37:12",
                "deleted_at": null,
                "human_status": "Fixed"
            },
            {
                "id": 5,
                "component_id": "3",
                "name": "Service Unavailable",
                "status": "2",
                "visible": 1,
                "message": "Server is not responding",
                "scheduled_at": "2018-02-26 10:05:03",
                "created_at": "2018-02-26 10:01:32",
                "updated_at": "2018-02-26 10:01:32",
                "deleted_at": null,
                "human_status": "Identified"
            },
            {
                "id": 6,
                "component_id": "2",
                "name": "Service Unavailable",
                "status": "2",
                "visible": 1,
                "message": "Server is not responding",
                "scheduled_at": "2018-02-26 10:05:03",
                "created_at": "2018-02-26 10:03:38",
                "updated_at": "2018-02-26 10:03:38",
                "deleted_at": null,
                "human_status": "Identified"
            }
        ]
    }

내가 원하는 결과는 모두 얻는 것입니다.ID~의 "구성요소 ID": "4", 그리고 이것을 출력합니다

"id" : 1,
"id" : 2,
"id" : 3,
"id" : 4,

내가 원하는 것은 구성 요소 ID를 grep하고 구성 요소 ID에 대한 모든 ID 값을 얻는 것입니다. 내 계획은 이러한 값을 내 for 루프로 가져오는 것이기 때문입니다.

답변1

사용jq:

curl -s --request GET \
    http://10.10.5.242/api/v1/incidents \
    -H "Content-Type: application/json;" \
    -H "X-Cachet-Token: ROvbssneyYwR8fwNgOWj" |
jq '.data[] | select(.component_id=="4").id'

질문에 인용한 JSON 형식으로 데이터가 전달된다고 가정하면

1
2
3
4

이는 JSON에서 4와 동일한 섹션 id의 개체 입니다.datacomponent_id

얻기 위해서는정밀한요청한 출력을 사용하십시오.

jq -r '.data[] | select(.component_id=="4") | "\"id\": \(.id),"'

특정 문서에 대해 다음이 생성됩니다.

"id": 1,
"id": 2,
"id": 3,
"id": 4,

답변2

다음을 사용할 수 있습니다.

grep -B1 "\"component_id\": \"4\"" | grep  "\"id\":" | sed 's/^ *//g'
  • B1: 경기 전 대사도 있어요
  • sed 's/^ *//g': 선행 및 후행 공백 제거

예:

echo sample | grep -B1 "\"component_id\": \"4\"" | grep  "\"id\":" | sed 's/^ *//g'

결과는 다음과 같습니다:

"id": 1,
"id": 2,
"id": 3,
"id": 4,

관련 정보