Shell

使用 for 循環遍歷 shell 腳本中的 JSON 數組

  • January 7, 2020

下面是 curl 命令輸出,需要腳本或命令列印“doc_count”小於 10 並將“key”保存在 output.txt 文件中。

{
 "took": 117,
 "timed_out": false,
 "_shards": {
   "total": 1005,
   "successful": 1005,
   "skipped": 979,
   "failed": 0
 },
 "hits": {
   "total": 205,
   "max_score": 0,
   "hits": []
 },
 "aggregations": {
   "2": {
     "doc_count": 205,
     "bg_count": 5778,
     "buckets": [
       {
         "key": "51.79.100.225",
         "doc_count": 24,
         "score": 3.1826769779892925,
         "bg_count": 24
       },
       {
         "key": "169.45.76.172",
         "doc_count": 13,
         "score": 1.7239500297442,
         "bg_count": 13
       },
       {
         "key": "198.147.22.235",
         "doc_count": 10,
         "score": 1.3261154074955384,
         "bg_count": 10
       },
       {
         "key": "5.178.86.74",
         "doc_count": 8,
         "score": 0.9386793575252826,
         "bg_count": 9
       },
       {
         "key": "35.193.189.85",
         "doc_count": 7,
         "score": 0.928280785246877,
         "bg_count": 7
       },
       {
         "key": "5.101.0.209",
         "doc_count": 6,
         "score": 0.7956692444973231,
         "bg_count": 6
       },
       {
         "key": "103.102.138.250",
         "doc_count": 4,
         "score": 0.5304461629982153,
         "bg_count": 4
       },
       {
         "key": "194.60.254.128",
         "doc_count": 4,
         "score": 0.5304461629982153,
         "bg_count": 4
       },
       {
         "key": "222.186.19.221",
         "doc_count": 4,
         "score": 0.5304461629982153,
         "bg_count": 4
       },
       {
         "key": "109.234.153.132",
         "doc_count": 4,
         "score": 0.5304461629982153,
         "bg_count": 4
       },
       {
         "key": "110.173.179.66",
         "doc_count": 4,
         "score": 0.5304461629982153,
         "bg_count": 4
       },
       {
         "key": "81.92.200.229",
         "doc_count": 4,
         "score": 0.5304461629982153,
         "bg_count": 4
       },
       {
         "key": "109.234.153.131",
         "doc_count": 4,
         "score": 0.5304461629982153,
         "bg_count": 4
       },
       {
         "key": "109.234.153.133",
         "doc_count": 3,
         "score": 0.39783462224866156,
         "bg_count": 3
       },
       {
         "key": "182.59.108.9",
         "doc_count": 3,
         "score": 0.39783462224866156,
         "bg_count": 3
       },
       {
         "key": "178.137.19.29",
         "doc_count": 6,
         "score": 0.3514721090925732,
         "bg_count": 13
       },
       {
         "key": "178.137.17.210",
         "doc_count": 3,
         "score": 0.23284711481261156,
         "bg_count": 5
       },
       {
         "key": "5.178.86.78",
         "doc_count": 3,
         "score": 0.19160023795359907,
         "bg_count": 6
       }
     ]
   }
 },
 "status": 200
}
jq -r '.aggregations."2".buckets[] | select(.doc_count < 100).key' file.json

這將選擇.aggregations."2".buckets[]數組中 a.doc_count小於 100 的元素,然後獲取.key每個元素的值。

將此結果重定向到文件以保存它。

引用自:https://unix.stackexchange.com/questions/560765