【发布时间】:2017-05-14 09:15:11
【问题描述】:
我在 bash shell 脚本中使用jq 每天处理 JSON 文件列表,将它们组合成一个新数组。组合它们后,我需要根据数组中每个对象的值重新计算值。然后在面向客户端的分析 HTML 报告中使用这个新的 JSON 文件。
我非常接近让它工作,但不知道如何组合我下面的jq 过滤器。
我可以在 Filter A 中创建一个重新计算的百分比数组,并且我可以在 Filter B 中输出我期望的数组(减去新的百分比值)但找不到将这些组合成Filter C的方法。
JQ 游乐场网址: https://jqplay.org/#
JSON input (located on gist.github)
过滤器 A:
{"hits_percents": [(.[].hits.count / (group_by(.hits.count) | map(map(.hits.count))|add|add)*100)], "visitors_percents": [(.[].visitors.count / (group_by(.visitors.count) | map(map(.visitors.count))|add|add)*100)], "bytes_percents": [(.[].bytes.count / (group_by(.bytes.count) | map(map(.bytes.count))|add|add)*100)]}
输出 A:
{
"hits_percents": [
1.2345679012345678,
8.641975308641975,
8.641975308641975,
9.876543209876543,
6.172839506172839,
3.7037037037037033,
1.2345679012345678,
2.4691358024691357,
58.0246913580247
],
"visitors_percents": [
4,
28.000000000000004,
28.000000000000004,
8,
4,
8,
4,
8,
8
],
"bytes_percents": [
0.31110007608374707,
36.59886623706793,
31.692110521802018,
7.187835244744665,
1.6285539319606,
5.880482040688714,
0.7481999111612437,
0.6484148671259253,
15.304437169365146
]
}
过滤器 B:
[.[] | { "hits": {"count": .hits.count, "percent": "TODO" }, "visitors": { "count": .visitors.count, "percent": "TODO"}, "bytes": {"count": .bytes.count, "percent": "TODO"}, "data": .data }]
过滤器 C:(过滤器 A + B)
????
任何关于如何使用jq 在 bash 脚本中获取 输出 C 的建议都很棒。我已经研究了好几个小时,但目前没有任何想法。
额外问题:我怎样才能让 jq 将百分比上的 2 位四舍五入?
我无法进行任何数学运算,他们的Math documentation 太模糊,无法提供帮助。
【问题讨论】: