Grep from a json formatted file

Hi All,

From a list of .gz files i am trying to get the list of files which have the below pattern. The pattern is "segment":"consumer".

{"@id":1,"aosProducts":[{"@id":2,"availability":{"@id":3,"shipInfo":{"@id":4,"key":"3","value":"3–5 business days"},"inStock":true},"context":{"@id":5,"geo":"US","lang":"en-us","segment":"consumer","channel":"common","format":"common","locale":"en_US"}

.

Tried grep and gave as below, but it displayed other files also with different second parameter like "segment:app","segment:periheral" etc. How can we obtain the correct pattern .

grep -Po '"segment": *\K"[^"]*"' *.*
1 Like

@nextStep, could you please try following.

grep -l -Po  '"segment":"consumer"'  *

with your shown samples it worked for me, also since you are saying you want filenames only so I added -l option with grep.

Thanks,
R. Singh

2 Likes

Thanks @RavinderSingh13. This worked

1 Like

@nextStep, glad that it helped you cheers and happy learning on this great forum :slight_smile: you could also mark that answer as Solution too,cheers :slight_smile:

Thanks,
R. Singh

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.