jq 和数学函数 [英] jq and Math functions

查看:31
本文介绍了jq 和数学函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从房地产数据库中检索 JSON.jq 可以很容易地拉出单独的属性/值,但有些值的单位不方便.例如,LotSize 变量以平方英尺为单位(需要除以 43560 得到英亩,这是更常规的),而 dateSold 是 Linux 时间戳.这是一个示例:

I'm retrieving JSON from a real estate database. jq makes it easy to pull out the separate properties/values, but some of the values are in inconvenient units. For example, the LotSize variable is in square feet (need to divide by 43560 to get acres, which is more conventional), and dateSold is a Linux timestamp. Here's a sample:

{
    "lotsize": 65340,
    "dateSold": 1207897200
} 

我希望能够对 jq 处理的值进行数学运算.我已经阅读了手册(https://stedolan.github.io/jq/manual/#数学)但它并没有让我知道如何去做.我想将上面的 JSON 数据转换成这样:

I'd like to be able to do math on values that jq processes. I've read the manual (https://stedolan.github.io/jq/manual/#Math) but it doesn't give me a sense of how to do it. I'd like to transform the JSON data above into something like this:

{
    "acres": 1.5,
    "soldOn": "Friday, April 11, 2008"
} 

我知道我可以修补 Excel,但是让 jq 无需任何进一步处理就可以做到这一点会很酷.关于这样做的任何想法?谢谢.

I know I can patch this up Excel, but it'd be cool to have jq do it without any further processing. Any thoughts on doing this? Thanks.

推荐答案

根据您的意见,

jq -c '{acres: (.lotsize/43560), soldOn: (.dateSold | strftime("%A %B %d, %Y")) }'

产生:

{"acres":1.5,"soldOn":"Friday April 11, 2008"}

jq 的最新版本支持环境变量 TZ,因此您可能需要查看 strflocaltime.

Recent versions of jq support the environment variable TZ so you might want to look at strflocaltime.

这篇关于jq 和数学函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆