2012-12-17 16 views
5

Ho bisogno di analizzare il jsonarray usando regex. My json isParsing JSON nello script di shell con regex

"keys": [ 
     { 
     "host": "example.com"  
     }, 
     { 
     "host": "example.net" 
     } 
    ] 

Ho bisogno di ottenere i due valori di host.

+0

Se fosse corretto json (ad esempio, avvolto in '{}'); potresti usare ['jq -r '.keys [] | .host''] (http://stedolan.github.io/jq/) – jfs

risposta

10

Quando si desidera estrarre il testo, grep è tuo amico:

grep -Po '(?<="host": ")[^"]*' myjsonFile 

Ad esempio:

kent$ echo '"keys": [ 
     { 
     "host": "example.com"  
     }, 
     { 
     "host": "example.net" 
     } 
    ]'|grep -Po '(?<="host": ")[^"]*' 

example.com 
example.net 
+2

Potresti aggiungere ulteriori spiegazioni a questo anwser? –

+5

non funziona su Mac – Andy

+0

@Andy ha bisogno di gnu grep. – Kent

1

La seguente espressione regolare sarà subito i valori host utilizzando un non-avido kleene jolly:

/"host":\s"(.*?)"/ 
Problemi correlati