json解析提取子字符串

时间:2019-03-07 22:19:03

标签: regex sed pivotal-cloud-foundry

我正在尝试从cloud Foundry env 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子json,我的sed有什么问题

 cf env myapp|sed 's/.*\(p-identity[^}].*}\).*/\1/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"
      }

2 个答案:

答案 0 :(得分:0)

对于您来说,将输出传递到grep以提取JSON,然后使用jq提取所需的字段可能更容易,例如:

cf env myapp | grep -oz'{。*}'| jq“您的过滤器在这里”

答案 1 :(得分:0)

我发现了一个肮脏的解决方法,虽然效率不高,但目前可以正常工作

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