在 jq 中使用指数幂

Using exponential powers in jq

我得到 JSON 个包含一个字节的对象,我正在转换为 TB。一般来说,我更喜欢这样做的方式是除以 1024 的 4 次方。

我正在使用 jq '.value|tonumber|(./(1024*1024*1024*1024))',但我更愿意将 1024*1024*1024*1024 替换为 1024**41024^4 之类的东西,无论如何在 jq?还有其他我想念的方法吗?

数据很简单,看起来像这样:

{
  "value": "43165913081459",
  "name": "AvailableStorage"
}

遗憾的是,数字只能进行基本的数学运算。但是,您应该可以访问 C Math functions available on your platform. So a lot of what's in the cstdlib should be fair game, such as the pow() 函数。

(.value | tonumber) / pow(1024; 4)

否则,除了将其作为参数传递外,我不知道还有其他方法。

$ jq --argjson div "$((1024**4))" '(.value | tonumber) / $div' input.json