相关疑难解决方法(0)

如何迭代包含bash空格的列表

你能告诉我如何迭代列表中的项目可以包含空格吗?

x=("some word", "other word", "third word")
for word in $x ; do
    echo -e "$word\n"
done
Run Code Online (Sandbox Code Playgroud)

如何强制输出:

some word
other word
third word
Run Code Online (Sandbox Code Playgroud)

代替:

some
word
(...)
third
word
Run Code Online (Sandbox Code Playgroud)

bash loops

6
推荐指数
1
解决办法
800
查看次数

使用 jq 对 JSON 中特定字段的值求平均值

我有以下 JSON 数据。

{
   "meta":{
      "earliest_ts":1601425980,
      "latest_ts":1601482740,
      "interval":"minutes",
      "level":"cluster",
      "entity_id":"xxxxx-xxxxx-xxxxx-xxxxx-xxxxx",
      "stat_labels":[
         "status_code_classes_per_workspace_total"
      ],
      "entity_type":"workspace",
      "start_ts":"1601425980"
   },
   "stats":{
      "cluster":{
         "1601431620":{
            "3xx":2,
            "4xx":87,
            "5xx":31,
            "2xx":352
         },
         "1601472780":{
            "3xx":14,
            "4xx":296,
            "5xx":2,
            "2xx":3811
         },
         "1601479140":{
            "3xx":17,
            "4xx":397,
            "5xx":19,
            "2xx":4399
         }
      }
   }
}
Run Code Online (Sandbox Code Playgroud)

我尝试对所有字段求平均值"3xx"

使用jq,我设法获取每个集群的密钥:

echo $data | jq -r '.stats.cluster|keys' | while read key; do 
  echo $key
done
Run Code Online (Sandbox Code Playgroud)

输出 :

[
"1601431620",
"1601472780",
"1601479140"
]
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试更进一步时,我无法进一步从每个字段检索数据。我从中得到了一些启发。

下面的代码不起作用,但你明白了:

 # total var will be used to calculate the average …
Run Code Online (Sandbox Code Playgroud)

bash parsing json jq

2
推荐指数
1
解决办法
647
查看次数

标签 统计

bash ×2

jq ×1

json ×1

loops ×1

parsing ×1