Shell 脚本 :: 如何在 grep 命令中放置多个过滤器

Shell Script :: How to put multiple filter in grep command

我当前的脚本正在为“useraccesses”下的所有块返回“userAccessId”值。下面我写了脚本和后端 json 响应。我已经过滤并获得“useraccesses”下所有块的“userAccessId”值。

但我的实际需求是,我想获得“numberOfAccessSchedules”值大于零的“userAccessId”值。 有人可以修改我的脚本吗?

MyScript.sh

#!/bin/sh
#

export TOKEN=$(curl -k -s -d "client_id=guard-public" -d "grant_type=password" -d "username=test" -d "password=test@123" "https://10.00.00.00/auth/realms/guard/protocol/openid-connect/token" | python -c "import sys, json; print json.load(sys.stdin)['access_token']")
    
#GET UserAccess ID

#echo "GET useraccess ID for all users"

curl -k --location --request GET "https://10.00.00.00:111/guardian-restservice/v1/useraccesses?page=$i&page_size=10000" -k -H "Authorization: bearer $TOKEN" | jq | grep "userAccessId" | awk -F : {'print '} | sed 's/,//' >> alluseraccessId.txt

后端JSON 没有过滤器的响应

{
  "page": {
    "currentPage": 1,
    "totalPages": 7,
    "pageSize": 10000
  },
  "useraccesses": [
    {
      "userAccessId": 918568786,
      "numberOfAccessSchedules": 2,
      "customAttributes": {},
      "networkElementUserId": ""
    },
    {
      "userAccessId": 918569013,
      "numberOfAccessSchedules": 1,
      "customAttributes": {},
      "networkElementUserId": ""
    },
    {
      "userAccessId": 918569301,
      "numberOfAccessSchedules": 0,
      "customAttributes": {},
      "networkElementUserId": ""
    },
    ...........
    ...........
    ...........
   ]
}

当前 O/P (alluseraccessId.txt)

918568786
918569013
918569301

预期 O/P (alluseraccessId.txt)

918568786
918569013

您可以使用 jq 轻松提取所需的值,这是在 shell 脚本中使用 JSON 的事实上的标准实用程序。尝试将基于正则表达式的工具应用于 JSON 或 XML 等结构化数据格式几乎总是一个坏主意。

curl ... | jq '.useraccesses[] | select(.numberOfAccessSchedules>0) | .userAccessId' >> alluseraccessId.txt

添加

918568786
918569013

到文件。

表示'for each element of the array in the useraccesses element of the top level object, if its numberOfAccessSchedules element is greater than 0, print the value of its userAccessId element.'