user1483747 user1483747 - 1 year ago 152
Linux Question

grep command to find the value of key from json

Please explain how to use grep command to extract some specified text from the following json -

{"result":"Customer information saved successfully with Customer id :59 and version :1","status":{"responseCode":200,"result":null,"responseMessage":"Success","responseType":"info"}}

After some googling, it looks like it is possible by using grep with a regular expression. But it is not working. Can you please point out the mistake

cat response.txt | grep -Po '(?<="Customer id ":)[0-9]+'

Assumption: response.txt contains the above json.

If yes please explain.

Answer Source

you missed K:

cat response.txt | grep -Po 'Customer id :\K[0-9]+'

screenshot, its working: enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download