[英]Shell Script Json, Mac
Here is my code 这是我的代码
function poke() {
json="curl -s -X GET http://pokeapi.co/api/v2/type/bug.json";
prop="half_damage_to"
temp=echo $json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $prop
echo ${temp##*|}
}
The problem here is that the terminal is only printing out the first JSON item, half_damage_to:[name|fighting
when it should be printing them all. 这里的问题是终端仅应打印出第一个JSON项目,
half_damage_to:[name|fighting
,而应将其全部打印出来。 How exactly do I get it to print all items in the JSON dictionary that are within half_damage_to
? 我如何确切地打印出
half_damage_to
内的JSON字典中的所有项目?
Use extended-regexp(grep -E)
to match on half_damage_to
array and then use awk
to split on half_damage_to
使用
extended-regexp(grep -E)
匹配half_damage_to
数组,然后使用awk
拆分half_damage_to
curl -s -X GET http://pokeapi.co/api/v2/type/bug.json | grep -oE '"half_damage_to":\[{.*?\}]' | awk 'BEGIN {FS="half_damage_to\":"} {print $2}'
Output: 输出:
[
{
"name":"fighting",
"url":"http://pokeapi.co/api/v2/type/2.json"
},
{
"name":"flying",
"url":"http://pokeapi.co/api/v2/type/3.json"
},
{
"name":"poison",
"url":"http://pokeapi.co/api/v2/type/4.json"
},
{
"name":"ghost",
"url":"http://pokeapi.co/api/v2/type/8.json"
},
{
"name":"steel",
"url":"http://pokeapi.co/api/v2/type/9.json"
},
{
"name":"fire",
"url":"http://pokeapi.co/api/v2/type/10.json"
},
{
"name":"fairy",
"url":"http://pokeapi.co/api/v2/type/18.json"
}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.