逻辑是这样的,我想判断每段 num_leechs
的值,如果大于 10,就返回 name
和 hash
的值,代码如下:
[{
"added_on": 1576120104,
"hash": "2fcb75646c688975daa840dfe466d21bc64118de",
"name": "Ad.Astra.2019",
"num_leechs": 545
},
{
"added_on": 1576120104,
"hash": "5c04a056d6821792db902b22aesdf23r2cd23rd23",
"name": "ABC",
"num_leechs": 32
},
{
"added_on": 1576120104,
"hash": "dfgsdfg34tv34tg34g34g6547h547h4567g567h45",
"name": "eGE",
"num_leechs": 2
}
]
1
noqwerty 2019-12-13 09:14:58 +08:00 via Android
用 jq 应该可以?
|
2
mcfog 2019-12-13 09:17:54 +08:00 via Android
jq 轻松搞定
|
3
wd 2019-12-13 09:19:40 +08:00 via iPhone
用 py 或者 perl import json 比较容易
|
4
wamson 2019-12-13 09:21:24 +08:00 via iPhone
Python 大法好
|
5
renmu 2019-12-13 09:26:40 +08:00 via Android
[{”hash”:row[hash], ”name”:row[name]]} for row in data if data[num_leechs>10]] 这是 py 写法
|
6
ddsfeng 2019-12-13 09:41:56 +08:00 1
因为不确定你说的 '就返回 name 和 hash 的值', 是哪种格式的, 所以弄了两个, 自行选择..
jq --> stedolan.github.io/jq/manual/ 1.json 为你帖的内容 ``` $ cat 1.json | jq '[.[] | select(.num_leechs > 10) | {name,hash}]' [ { "name": "Ad.Astra.2019", "hash": "2fcb75646c688975daa840dfe466d21bc64118de" }, { "name": "ABC", "hash": "5c04a056d6821792db902b22aesdf23r2cd23rd23" } ] ``` ``` $ cat 1.json | jq '[.[] | select(.num_leechs > 10) | [.name,.hash]]' [ [ "Ad.Astra.2019", "2fcb75646c688975daa840dfe466d21bc64118de" ], [ "ABC", "5c04a056d6821792db902b22aesdf23r2cd23rd23" ] ] ``` |
8
opticaline 2019-12-13 10:35:39 +08:00
cat temp | grep num_leechs -B 2 | awk -v RS="--" '{if($6 > 10){print $4,$2}}' | awk -F '[", ]' '{print "name="$2",hash="$6}'
name=Ad.Astra.2019,hash=2fcb75646c688975daa840dfe466d21bc64118de name=ABC,hash=5c04a056d6821792db902b22aesdf23r2cd23rd23 |
9
geelaw 2019-12-13 10:42:57 +08:00 via iPhone
最简单且可维护的之一是用 PowerShell
Get-Content | ConvertFrom-Json | Write-Output | Where-Object num_leechs -gt 10 | Select-Object name, hash |
10
Tink 2019-12-13 11:15:40 +08:00 via iPhone
jq
|
11
ddsfeng 2019-12-13 12:37:56 +08:00
|
12
ddsfeng 2019-12-13 12:38:36 +08:00
|
13
ETiV 2019-12-13 12:42:44 +08:00 via iPhone
jq
自己多看看文档,你就能不求人了(或者可以帮助别人) |