Is possible get data for one specific product, closed in { }
, from json, by EAN
, all data are in one line?
Example
Input.
...
{ "article": {"code": "01333457004","name": "ALAZANIS VALLEY 2015","note": "ČERV VÍNO EVROPA VÝCH OSTATNÍ","sel_unit": "Kus","unit_price": 229.0,"category": "ČERVENÉ,POLOSLADKÉ","unit": "L","EAN": "4867601700052","unit_volume": 0.75,"producer": null,"tax": 21.0,"text": "Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;","is_action": "1","action_from": "20190905","action_to": "20190918","ordered_from": "20190126","ordered_to": "20190830","shelf_id": "1030542","is_outlet": 0}},{ "article" ...
...
Expected output (command selected by code
01333457004
), via sed
or awk
?
{ "article": {"code": "01333457004","name": "ALAZANIS VALLEY 2015","note": "ČERV VÍNO EVROPA VÝCH OSTATNÍ","sel_unit": "Kus","unit_price": 229.0,"category": "ČERVENÉ,POLOSLADKÉ","unit": "L","EAN": "4867601700052","unit_volume": 0.75,"producer": null,"tax": 21.0,"text": "Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;Alazanis Valley 2015;Gruzie,Kachetie;červené polsladké;750ml;16°C;","is_action": "1","action_from": "20190905","action_to": "20190918","ordered_from": "20190126","ordered_to": "20190830","shelf_id": "1030542","is_outlet": 0}}
Thank you.
I would just split the file into one entry per line first. That makes parsing it much simpler. So, find all cases of
},{
and add a newline breaking them:Now, you can just search for your code. But use the
-w
flag forgrep
which makes it look for "words" to avoid finding the code as a substring. So if you have one line with code01333457004
and another line with code013334570041
, searching for01333457004
won't find013334570041
.Putting all this together gives:
If you really want to do it in a single operation, you can try using grep with
-P
for PCRE regular expressions which support lookaheads:This will look for
{ "article":
, then the shortest stretch of 0 or more characters (.*?
) until the string01333457004
, but only if that appears surrounded by non-word characters, so only if it is a word; that's what the\b
mean, then the shortest stretch of 0 or more characters again that end with a}
followed by,}
.You can use jq.
Have a look at this
Probably in your case it should be something like: