forループを使用してシェルスクリプトでJSON配列を繰り返します。

forループを使用してシェルスクリプトでJSON配列を繰り返します。

以下はカールコマンドの出力です。 「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
}

ベストアンサー1

jq -r '.aggregations."2".buckets[] | select(.doc_count < 100).key' file.json

.aggregations."2".buckets[]次に、配列内のaが100未満の要素を選択し、.doc_count.key要素の値を取得します。

結果をファイルにリダイレクトして保存します。

おすすめ記事