如何使用 grep 多个模式并删除第一个模式

如何使用 grep 多个模式并删除第一个模式

我尝试过的代码没有得到我想要的确切输出。这是我尝试过的代码:

   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,

我想要的是 grep the component_id 并将获取组件 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,

相关内容