-1

PROMETHEUS_METRIC: { name: sc_pcd_g2version, container: mgmtbroker, endpoint: vxexporter, instance: 10.42.24.132:9116, job: prometheus, namespace: delivery, pod: chn1-d1-ac-0, service: prometheus }{ name: sc_pcd_g2version, container: mgmtbroker, endpoint: vxexporter, instance: 10.42.3.110:9116, job: prometheus, namespace: delivery, pod: chn1-d1-ia-0, service: prometheus }

Its json format. How to split the above response into 2 as below in shell script. I have variable PROMETHEUS_METRIC have the above json.

{ __name__: sc_pcd_g2version, container: mgmtbroker, endpoint: vxexporter, instance: 10.42.24.132:9116, job: prometheus, namespace: delivery, pod: chn1-d1-ac-0, service: prometheus }

{ __name__: sc_pcd_g2version, container: mgmtbroker, endpoint: vxexporter, instance: 10.42.3.110:9116, job: prometheus, namespace: delivery, pod: chn1-d1-ia-0, service: prometheus }
ramesh k
  • 23
  • 6
  • 3
    Please edit your question (no comment): What have you searched for, and what did you find? What have you tried, and how did it fail? – Cyrus May 19 '23 at 20:05
  • Where does this come from? From a file or from stdin? – Cyrus May 19 '23 at 20:06
  • This is response from a file and it return response of two metrics in {} which needs to be iterated and splitted – ramesh k May 19 '23 at 20:09

1 Answers1

0

Neither the input nor the desired output is valid JSON. Thus, treating input and output as raw text (using jq with flags -Rr, although at this point you could use any JSON-unrelated text-processing tool), you could simply scan by regular expressions, for instance from { to }:

jq -Rr 'scan("({[^}]*})")[]'
{ __name__: sc_pcd_g2version, container: mgmtbroker, endpoint: vxexporter, instance: 10.42.24.132:9116, job: prometheus, namespace: delivery, pod: chn1-d1-ac-0, service: prometheus }
{ __name__: sc_pcd_g2version, container: mgmtbroker, endpoint: vxexporter, instance: 10.42.3.110:9116, job: prometheus, namespace: delivery, pod: chn1-d1-ia-0, service: prometheus }

Demo

pmf
  • 24,478
  • 2
  • 22
  • 31
  • I think it makes sense to wait with an answer until the questioner has shown what he has tried to answer the question himself. – Cyrus May 19 '23 at 20:17