user1483747 user1483747 - 2 months ago 24
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

you missed K:

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

screenshot, its working: enter image description here