jq dates and unix timestamps

前端 未结 2 807
被撕碎了的回忆
被撕碎了的回忆 2021-02-02 07:32

So I have a data with bunch of unix timestamp values (in milliseconds). Something like this:

{
    \"id\": \"f6922fd5-4f97-4113-820e-b45eba0ae236\",
    \"publis         


        
2条回答
  •  庸人自扰
    2021-02-02 08:27

    Sure! Your provided input is not valid JSON, but I'm going to assume the trailing commas on those objects are removed and the objects are wrapped in an array, which would be the root object of the JSON document.

    First, we can transform the millisecond-precision UNIX dates into second-precision, which is what jq's date functions expect, and then convert that to the human-readable dates you expect:

    .[].published_at |= (. / 1000 | strftime("%Y-%m-%d"))
    

    Then, we select only those elements whose dates match:

    map(select(.published_at == $date))
    

    Lastly, we put it all together, taking the $date variable from the command-line:

    jq --arg date "2016-04-25" '.[].published_at |= (. / 1000 | strftime("%Y-%m-%d")) | map(select(.published_at == $date))' stuff.json
    

提交回复
热议问题