How to parse with Bash script? (platform MIPS)? [duplicate]
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
This question already has an answer here:
Parse JSON using Python?
8 answers
$ curl -LNs "http://urladrescom/content.json" > content.json
content.json
"k":[
"i":1,
"n":"NAME 1",
"p":[
"b":"Event 1",
"c":"00:00",
"d":"03:00"
,
"b":"Event 2",
"c":"23:00",
"d":"00:00"
]
,
"i":2,
"n":"NAME 2",
"p":[
"b":"Event 1",
"c":"07:15",
"d":"09:15"
,
"b":"Event 2",
"c":"22:00",
"d":"23:15"
,
"b":"Event 3",
"c":"23:15",
"d":"02:30"
]
,
"i":3,
"n":"NAME 3",
"p":[
"b":"Event 1",
"c":"07:15",
"d":"09:15"
,
"b":"Event 2",
"c":"22:00",
"d":"23:15"
,
"b":"Event 3",
"c":"23:15",
"d":"02:30"
]
]
I want to get the content in "NAME 2" under Event 1,2,3 with a Bash script (grep
, awk
, sed
, etc..) (or Python in a Bash script cmd).
I want print result :
NAME 2 Event 1 07:15 09:15
NAME 2 Event 2 22:00 23:15
NAME 2 Event 3 23:15 02:30
awk sed grep json
marked as duplicate by G-Man, msp9011, Wouter Verhelst, Isaac, dhag Aug 20 at 19:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
1
down vote
favorite
This question already has an answer here:
Parse JSON using Python?
8 answers
$ curl -LNs "http://urladrescom/content.json" > content.json
content.json
"k":[
"i":1,
"n":"NAME 1",
"p":[
"b":"Event 1",
"c":"00:00",
"d":"03:00"
,
"b":"Event 2",
"c":"23:00",
"d":"00:00"
]
,
"i":2,
"n":"NAME 2",
"p":[
"b":"Event 1",
"c":"07:15",
"d":"09:15"
,
"b":"Event 2",
"c":"22:00",
"d":"23:15"
,
"b":"Event 3",
"c":"23:15",
"d":"02:30"
]
,
"i":3,
"n":"NAME 3",
"p":[
"b":"Event 1",
"c":"07:15",
"d":"09:15"
,
"b":"Event 2",
"c":"22:00",
"d":"23:15"
,
"b":"Event 3",
"c":"23:15",
"d":"02:30"
]
]
I want to get the content in "NAME 2" under Event 1,2,3 with a Bash script (grep
, awk
, sed
, etc..) (or Python in a Bash script cmd).
I want print result :
NAME 2 Event 1 07:15 09:15
NAME 2 Event 2 22:00 23:15
NAME 2 Event 3 23:15 02:30
awk sed grep json
marked as duplicate by G-Man, msp9011, Wouter Verhelst, Isaac, dhag Aug 20 at 19:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
I'd suggest usingjq
instead e.g.jq -r --arg name "NAME 2" '.k | select(.n==$name) | .p | [$name, .b, .c, .d] | @tsv' content.json
â steeldriver
Aug 19 at 23:43
1
jq is not in my system. my system platform mips only grep sed awk curl and cmd python !
â tioma
Aug 20 at 0:02
@tioma You should be able to installjq
, either by using your package manager, or by compiling it yourself.
â Kusalananda
Aug 20 at 7:31
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This question already has an answer here:
Parse JSON using Python?
8 answers
$ curl -LNs "http://urladrescom/content.json" > content.json
content.json
"k":[
"i":1,
"n":"NAME 1",
"p":[
"b":"Event 1",
"c":"00:00",
"d":"03:00"
,
"b":"Event 2",
"c":"23:00",
"d":"00:00"
]
,
"i":2,
"n":"NAME 2",
"p":[
"b":"Event 1",
"c":"07:15",
"d":"09:15"
,
"b":"Event 2",
"c":"22:00",
"d":"23:15"
,
"b":"Event 3",
"c":"23:15",
"d":"02:30"
]
,
"i":3,
"n":"NAME 3",
"p":[
"b":"Event 1",
"c":"07:15",
"d":"09:15"
,
"b":"Event 2",
"c":"22:00",
"d":"23:15"
,
"b":"Event 3",
"c":"23:15",
"d":"02:30"
]
]
I want to get the content in "NAME 2" under Event 1,2,3 with a Bash script (grep
, awk
, sed
, etc..) (or Python in a Bash script cmd).
I want print result :
NAME 2 Event 1 07:15 09:15
NAME 2 Event 2 22:00 23:15
NAME 2 Event 3 23:15 02:30
awk sed grep json
This question already has an answer here:
Parse JSON using Python?
8 answers
$ curl -LNs "http://urladrescom/content.json" > content.json
content.json
"k":[
"i":1,
"n":"NAME 1",
"p":[
"b":"Event 1",
"c":"00:00",
"d":"03:00"
,
"b":"Event 2",
"c":"23:00",
"d":"00:00"
]
,
"i":2,
"n":"NAME 2",
"p":[
"b":"Event 1",
"c":"07:15",
"d":"09:15"
,
"b":"Event 2",
"c":"22:00",
"d":"23:15"
,
"b":"Event 3",
"c":"23:15",
"d":"02:30"
]
,
"i":3,
"n":"NAME 3",
"p":[
"b":"Event 1",
"c":"07:15",
"d":"09:15"
,
"b":"Event 2",
"c":"22:00",
"d":"23:15"
,
"b":"Event 3",
"c":"23:15",
"d":"02:30"
]
]
I want to get the content in "NAME 2" under Event 1,2,3 with a Bash script (grep
, awk
, sed
, etc..) (or Python in a Bash script cmd).
I want print result :
NAME 2 Event 1 07:15 09:15
NAME 2 Event 2 22:00 23:15
NAME 2 Event 3 23:15 02:30
This question already has an answer here:
Parse JSON using Python?
8 answers
awk sed grep json
awk sed grep json
edited Aug 20 at 16:44
slmâ¦
238k65493664
238k65493664
asked Aug 19 at 23:14
tioma
134
134
marked as duplicate by G-Man, msp9011, Wouter Verhelst, Isaac, dhag Aug 20 at 19:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by G-Man, msp9011, Wouter Verhelst, Isaac, dhag Aug 20 at 19:37
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
I'd suggest usingjq
instead e.g.jq -r --arg name "NAME 2" '.k | select(.n==$name) | .p | [$name, .b, .c, .d] | @tsv' content.json
â steeldriver
Aug 19 at 23:43
1
jq is not in my system. my system platform mips only grep sed awk curl and cmd python !
â tioma
Aug 20 at 0:02
@tioma You should be able to installjq
, either by using your package manager, or by compiling it yourself.
â Kusalananda
Aug 20 at 7:31
add a comment |Â
2
I'd suggest usingjq
instead e.g.jq -r --arg name "NAME 2" '.k | select(.n==$name) | .p | [$name, .b, .c, .d] | @tsv' content.json
â steeldriver
Aug 19 at 23:43
1
jq is not in my system. my system platform mips only grep sed awk curl and cmd python !
â tioma
Aug 20 at 0:02
@tioma You should be able to installjq
, either by using your package manager, or by compiling it yourself.
â Kusalananda
Aug 20 at 7:31
2
2
I'd suggest using
jq
instead e.g. jq -r --arg name "NAME 2" '.k | select(.n==$name) | .p | [$name, .b, .c, .d] | @tsv' content.json
â steeldriver
Aug 19 at 23:43
I'd suggest using
jq
instead e.g. jq -r --arg name "NAME 2" '.k | select(.n==$name) | .p | [$name, .b, .c, .d] | @tsv' content.json
â steeldriver
Aug 19 at 23:43
1
1
jq is not in my system. my system platform mips only grep sed awk curl and cmd python !
â tioma
Aug 20 at 0:02
jq is not in my system. my system platform mips only grep sed awk curl and cmd python !
â tioma
Aug 20 at 0:02
@tioma You should be able to install
jq
, either by using your package manager, or by compiling it yourself.â Kusalananda
Aug 20 at 7:31
@tioma You should be able to install
jq
, either by using your package manager, or by compiling it yourself.â Kusalananda
Aug 20 at 7:31
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
2
down vote
accepted
You can use this Python to do what you want:
$ cat parse.py
#!/bin/python
import json
#from pprint import pprint
with open('content.json') as f:
data = json.load(f)
for dict in data["k"]:
if (dict["n"] == "NAME 2"):
for elem in dict["p"]:
print(dict["n"] + ' \ ' + elem["b"] + ' \ ' + elem["c"] + ' \ ' + elem["d"])
Example
$ ./parse.py
NAME 2 Event 1 07:15 09:15
NAME 2 Event 2 22:00 23:15
NAME 2 Event 3 23:15 02:30
this work thank you
â tioma
Aug 20 at 16:37
add a comment |Â
up vote
0
down vote
Using the command line JSON parser jq
...
There might be a shorter jq
expression that does this, but this is what I came up with:
$ jq -r --arg name "NAME 2" '.|select(.n==$name).p|[$name,.]|join(" \ ")' content.json
NAME 2 Event 1 07:15 09:15
NAME 2 Event 2 22:00 23:15
NAME 2 Event 3 23:15 02:30
Or, using slightly more selective code, as suggested by steeldriver,
$ jq -r --arg name "NAME 2" '.k|select(.n==$name).p|[$name, .b, .c, .d]|join(" \ ")' content.json
NAME 2 Event 1 07:15 09:15
NAME 2 Event 2 22:00 23:15
NAME 2 Event 3 23:15 02:30
name
is a jq
variable whose value is passed on the command line. The jq
code is doing five things:
- Extract the
k
arrays in the data that we're interested in. - Select the particular array that we want to output (and the
p
bit from this). - Create an array for outputting (since the name is not part of the data, it has to be inserted).
- Do output with the particular delimiter inserted.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
You can use this Python to do what you want:
$ cat parse.py
#!/bin/python
import json
#from pprint import pprint
with open('content.json') as f:
data = json.load(f)
for dict in data["k"]:
if (dict["n"] == "NAME 2"):
for elem in dict["p"]:
print(dict["n"] + ' \ ' + elem["b"] + ' \ ' + elem["c"] + ' \ ' + elem["d"])
Example
$ ./parse.py
NAME 2 Event 1 07:15 09:15
NAME 2 Event 2 22:00 23:15
NAME 2 Event 3 23:15 02:30
this work thank you
â tioma
Aug 20 at 16:37
add a comment |Â
up vote
2
down vote
accepted
You can use this Python to do what you want:
$ cat parse.py
#!/bin/python
import json
#from pprint import pprint
with open('content.json') as f:
data = json.load(f)
for dict in data["k"]:
if (dict["n"] == "NAME 2"):
for elem in dict["p"]:
print(dict["n"] + ' \ ' + elem["b"] + ' \ ' + elem["c"] + ' \ ' + elem["d"])
Example
$ ./parse.py
NAME 2 Event 1 07:15 09:15
NAME 2 Event 2 22:00 23:15
NAME 2 Event 3 23:15 02:30
this work thank you
â tioma
Aug 20 at 16:37
add a comment |Â
up vote
2
down vote
accepted
up vote
2
down vote
accepted
You can use this Python to do what you want:
$ cat parse.py
#!/bin/python
import json
#from pprint import pprint
with open('content.json') as f:
data = json.load(f)
for dict in data["k"]:
if (dict["n"] == "NAME 2"):
for elem in dict["p"]:
print(dict["n"] + ' \ ' + elem["b"] + ' \ ' + elem["c"] + ' \ ' + elem["d"])
Example
$ ./parse.py
NAME 2 Event 1 07:15 09:15
NAME 2 Event 2 22:00 23:15
NAME 2 Event 3 23:15 02:30
You can use this Python to do what you want:
$ cat parse.py
#!/bin/python
import json
#from pprint import pprint
with open('content.json') as f:
data = json.load(f)
for dict in data["k"]:
if (dict["n"] == "NAME 2"):
for elem in dict["p"]:
print(dict["n"] + ' \ ' + elem["b"] + ' \ ' + elem["c"] + ' \ ' + elem["d"])
Example
$ ./parse.py
NAME 2 Event 1 07:15 09:15
NAME 2 Event 2 22:00 23:15
NAME 2 Event 3 23:15 02:30
answered Aug 20 at 6:29
slmâ¦
238k65493664
238k65493664
this work thank you
â tioma
Aug 20 at 16:37
add a comment |Â
this work thank you
â tioma
Aug 20 at 16:37
this work thank you
â tioma
Aug 20 at 16:37
this work thank you
â tioma
Aug 20 at 16:37
add a comment |Â
up vote
0
down vote
Using the command line JSON parser jq
...
There might be a shorter jq
expression that does this, but this is what I came up with:
$ jq -r --arg name "NAME 2" '.|select(.n==$name).p|[$name,.]|join(" \ ")' content.json
NAME 2 Event 1 07:15 09:15
NAME 2 Event 2 22:00 23:15
NAME 2 Event 3 23:15 02:30
Or, using slightly more selective code, as suggested by steeldriver,
$ jq -r --arg name "NAME 2" '.k|select(.n==$name).p|[$name, .b, .c, .d]|join(" \ ")' content.json
NAME 2 Event 1 07:15 09:15
NAME 2 Event 2 22:00 23:15
NAME 2 Event 3 23:15 02:30
name
is a jq
variable whose value is passed on the command line. The jq
code is doing five things:
- Extract the
k
arrays in the data that we're interested in. - Select the particular array that we want to output (and the
p
bit from this). - Create an array for outputting (since the name is not part of the data, it has to be inserted).
- Do output with the particular delimiter inserted.
add a comment |Â
up vote
0
down vote
Using the command line JSON parser jq
...
There might be a shorter jq
expression that does this, but this is what I came up with:
$ jq -r --arg name "NAME 2" '.|select(.n==$name).p|[$name,.]|join(" \ ")' content.json
NAME 2 Event 1 07:15 09:15
NAME 2 Event 2 22:00 23:15
NAME 2 Event 3 23:15 02:30
Or, using slightly more selective code, as suggested by steeldriver,
$ jq -r --arg name "NAME 2" '.k|select(.n==$name).p|[$name, .b, .c, .d]|join(" \ ")' content.json
NAME 2 Event 1 07:15 09:15
NAME 2 Event 2 22:00 23:15
NAME 2 Event 3 23:15 02:30
name
is a jq
variable whose value is passed on the command line. The jq
code is doing five things:
- Extract the
k
arrays in the data that we're interested in. - Select the particular array that we want to output (and the
p
bit from this). - Create an array for outputting (since the name is not part of the data, it has to be inserted).
- Do output with the particular delimiter inserted.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Using the command line JSON parser jq
...
There might be a shorter jq
expression that does this, but this is what I came up with:
$ jq -r --arg name "NAME 2" '.|select(.n==$name).p|[$name,.]|join(" \ ")' content.json
NAME 2 Event 1 07:15 09:15
NAME 2 Event 2 22:00 23:15
NAME 2 Event 3 23:15 02:30
Or, using slightly more selective code, as suggested by steeldriver,
$ jq -r --arg name "NAME 2" '.k|select(.n==$name).p|[$name, .b, .c, .d]|join(" \ ")' content.json
NAME 2 Event 1 07:15 09:15
NAME 2 Event 2 22:00 23:15
NAME 2 Event 3 23:15 02:30
name
is a jq
variable whose value is passed on the command line. The jq
code is doing five things:
- Extract the
k
arrays in the data that we're interested in. - Select the particular array that we want to output (and the
p
bit from this). - Create an array for outputting (since the name is not part of the data, it has to be inserted).
- Do output with the particular delimiter inserted.
Using the command line JSON parser jq
...
There might be a shorter jq
expression that does this, but this is what I came up with:
$ jq -r --arg name "NAME 2" '.|select(.n==$name).p|[$name,.]|join(" \ ")' content.json
NAME 2 Event 1 07:15 09:15
NAME 2 Event 2 22:00 23:15
NAME 2 Event 3 23:15 02:30
Or, using slightly more selective code, as suggested by steeldriver,
$ jq -r --arg name "NAME 2" '.k|select(.n==$name).p|[$name, .b, .c, .d]|join(" \ ")' content.json
NAME 2 Event 1 07:15 09:15
NAME 2 Event 2 22:00 23:15
NAME 2 Event 3 23:15 02:30
name
is a jq
variable whose value is passed on the command line. The jq
code is doing five things:
- Extract the
k
arrays in the data that we're interested in. - Select the particular array that we want to output (and the
p
bit from this). - Create an array for outputting (since the name is not part of the data, it has to be inserted).
- Do output with the particular delimiter inserted.
edited Aug 20 at 6:47
answered Aug 20 at 6:24
Kusalananda
106k14209329
106k14209329
add a comment |Â
add a comment |Â
2
I'd suggest using
jq
instead e.g.jq -r --arg name "NAME 2" '.k | select(.n==$name) | .p | [$name, .b, .c, .d] | @tsv' content.json
â steeldriver
Aug 19 at 23:43
1
jq is not in my system. my system platform mips only grep sed awk curl and cmd python !
â tioma
Aug 20 at 0:02
@tioma You should be able to install
jq
, either by using your package manager, or by compiling it yourself.â Kusalananda
Aug 20 at 7:31