4

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.

Kevin
  • 53,822
  • 15
  • 101
  • 132
user1483747
  • 63
  • 2
  • 3
  • 5

2 Answers2

6

you missed K:

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

screenshot, its working: enter image description here

Hackaholic
  • 19,069
  • 5
  • 54
  • 72
  • 2
    grep can read files on it's own, so `grep ... file` (useless use of cat). And the `\K` is a shorthand for what the OP use already, positive look-behind – Gilles Quénot Dec 27 '14 at 18:22
  • it is working....i had missed some text...Can u please explain it in details ..or can u give a link where can i find the detail documentation regarding it . – user1483747 Dec 27 '14 at 18:33
  • Can you explain what K does here? – shajin Apr 23 '18 at 11:53
0

Try doing this :

$ jq -r '.result' json_file | grep -oP '(?<=Customer id :)\d+'
59

Check http://stedolan.github.io/jq/manual/

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223