json 解析提取子字符串

json parse extract substring

我正在尝试从 Cloud Foundry 环境中提取凭证客户端机密 json 字符串

cf env myapp

给出了确切的以下内容,(不是正确的 json 所以这就是我不能使用 jq 的原因)

Getting env variables for app icm in org myorg / space myspace as 
xxyy...
OK


{
  "myenv_env_json": {
"http_proxy": "http://mycompany-svr-proxy-qa.mycompany.com:7070",
"https_proxy": "http://mycompany-svr-proxy-qa.mycompany.com:7070",
"no_proxy": "*.mycompany.com"
  },
  "running_env_json": {},
  "system_env_json": {
"VCAP_SERVICES": {
  "user-provided": [
    {
      "name": "myapp-parameters",
      "instance_name": "myapp-parameters",
      "binding_name": null,
      "credentials": {
        "auth-domain": "https://sso.login.run-np.mycompany.com",
        "backend-url-other": "https://myservice-other.apps-np.mycompany.com",
        "client-secret": "121322332-32322-23232-232-32-23232",
        "stage": "mystg",
        "backend-url": "https://myservice-other.apps-np.mycompany.com",
        "client-secret-other": "121322332-32322-23232-232-32-23232"
      },
      "syslog_drain_url": "",
      "volume_mounts": [],
      "label": "user-provided",
      "tags": []
    },
    {
      "name": "appdynamics",
      "instance_name": "appdynamics",
      "binding_name": null,
      "credentials": {
        "account-access-key": "1213232-232-322-2322323-2323232-311",
        "account-name": "customer1",
        "application-name": "myenv-dev",
        "host-name": "appdx-qa.mycompany.com",
        "node-name": "$(ruby -e \"require 'json'; a = JSON.parse(ENV['VCAP_APPLICATION']); puts \\"#{a['application_name']}-#{a['cf_api'].split(/\.|-/)[2]}:#{a['instance_index']}\\"\")",
        "port": "9401",
        "ssl-enabled": "true",
        "tier-name": "$(ruby -e \"require 'json'; a = JSON.parse(ENV['VCAP_APPLICATION']); puts \\"#{a['application_name']}-#{a['cf_api'].split(/\.|-/)[2]}\\"\")",
        "version": "4.2.7_1"
      },
      "syslog_drain_url": "",
      "volume_mounts": [],
      "label": "user-provided",
      "tags": []
    }
  ],
  "p-identity": [
    {
      "name": "sso",
      "instance_name": "sso",
      "binding_name": null,
      "credentials": {
        "auth_domain": "https://sso.login.run-np.mycompany.com",
        "client_secret": "123232-23232-2323243-242323r3r",
        "client_id": "afdvdf-dvdfdd-fgdgdf-d23232"
      },
      "syslog_drain_url": null,
      "volume_mounts": [],
      "label": "p-identity",
      "provider": null,
      "plan": "sso",
      "tags": []
    }
  ]
}
 },
 "application_env_json": {
 "VCAP_APPLICATION": {
  "cf_api": "https://api.run-np.mycompany.com",
  "limits": {
    "fds": 16384
  },
  "application_name": "myapp",
  "application_uris": [
    "myapp-dev.apps-np.mycompany.com"
  ],
  "name": "myapp",
  "space_name": "myapp-dev",
  "space_id": "392929-23223-2323-2322-2322",
  "uris": [
    "myapp-dev.apps-np.mycompany.com"
  ],
  "users": null,
  "application_id": "fwew78cc-wewc5c-dfd8a7-89d5-fdfefwewb"
}
 }
 }

 User-Provided:
 APP_ENV: development
 GRANT_TYPE: authorization_code
 SSO_AUTO_APPROVED_SCOPES: openid
 SSO_IDENTITY_PROVIDERS: mycompany-single-signon
 SSO_LAUNCH_URL: https://myapp-dev.apps-np.mycompany.com/
 SSO_REDIRECT_URIS: https://myapp-dev.apps-np.mycompany.com/callback,http://myapp-dev.apps-np.mycompany.com/callback
 SSO_SCOPES: openid,user_attributes
 callback_url: https://myapp-dev.apps-np.mycompany.com/callback
 client_secret: secret
 client_secret_other: secretother

 No running env variables have been set

 Staging Environment Variable Groups:
 http_proxy: http://myapp-svr-proxy-qa.mycompany.com:7070
 https_proxy: http://myapp-svr-proxy-qa.mycompany.com:7070
 no_proxy: *.mycompany.com

这是我正在尝试使用的,到目前为止,我没有成功提取 p-identity sub json,我的 sed 有什么问题

 cf env myapp|sed 's/.*\(p-identity[^}].*}\).*//p'

我的预期输出应该如下

"p-identity": [
    {
      "name": "sso",
      "instance_name": "sso",
      "binding_name": null,
      "credentials": {
        "auth_domain": "https://sso.login.run-np.mycompany.com",
        "client_secret": "123232-23232-2323243-242323r3r",
        "client_id": "afdvdf-dvdfdd-fgdgdf-d23232"
      }

对于您的情况,将输出通过管道传输到 grep 以提取 JSON 可能更容易,然后使用 jq 提取您想要的字段,例如:

cf env myapp | grep -oz '{.*}' | jq 'your filter here'

我找到了一个肮脏的解决方法,可能效率不高但现在有效

cf env myapp|sed 1,4d|sed -n '/User-Provided:/q;p'|jq -c -r '.VCAP_SERVICES."p-identity"[0].credentials.client_secret'| head -n1