代码之家  ›  专栏  ›  技术社区  ›  kingster

对jq中的最后一列应用strftime

  •  1
  • kingster  · 技术社区  · 6 年前

    我正在处理一个文件并从中筛选出某些字段。然后,我将整个结果转换为CSV文件。然而,我过滤掉的最后一列是历元时间戳(最长可达毫秒)。

    四处搜索,我发现我可以使用 strftime 但是,jq中用于转换的函数;应用时 | strftime 在整个数组中,它将函数应用于所有值。

    TZ='Asia/Kolkata'  grep $id ~/hhfh.log  |  grep -oE '\{.+' | jq  -r '[.highPrice, .lowPrice, .openPrice, .closePrice, .volumeTradedToday, .totalBuyQuantity, .totalSellQuantity, .tickTimestamp | strftime("%B %d %Y %I:%M%p %Z")]' | jq -r 'map(tostring) | join(", ")'
    

    我可以修改它以便 strftime("%B %d %Y %I:%M%p %Z") 仅适用于 .tickTimestmap 价值如果不是,我会依靠 awk ?


    记录来自的行 hhfh.log :

    2018-03-06 03:30:04,938 DEBUG KiteTickerSourceLogic [ReadingThread] TickData: {"mode":"full","tradable":false,"instrumentToken":2997505,"lastTradedPrice":740.0,"highPrice":0.0,"lowPrice":0.0,"openPrice":731.5,"closePrice":739.9,"change":0.01351533991080183,"lastTradedQuantity":23.0,"averageTradePrice":0.0,"volumeTradedToday":12.0,"totalBuyQuantity":285.0,"totalSellQuantity":1469.0,"lastTradedTime":1520245282000,"oi":0.0,"tickTimestamp":1520307004000,"openInterestDayHigh":0.0,"openInterestDayLow":0.0,"marketDepth":{"buy":[{"quantity":1,"price":735.55,"orders":1},{"quantity":86,"price":731.5,"orders":1},{"quantity":168,"price":731.0,"orders":1},{"quantity":25,"price":730.1,"orders":1},{"quantity":0,"price":0.0,"orders":0}],"sell":[{"quantity":550,"price":743.6,"orders":1},{"quantity":550,"price":746.6,"orders":1},{"quantity":10,"price":750.0,"orders":1},{"quantity":25,"price":777.0,"orders":1},{"quantity":12,"price":-0.01,"orders":1}]}}
    

    我当前生成的内容:

    January 01 1970 12:05:08AM UTC, January 01 1970 12:05:02AM UTC, January 01 1970 12:05:04AM UTC, January 01 1970 12:05:06AM UTC, January 19 1970 08:56:53AM UTC, January 01 1970 04:39:20AM UTC, January 01 1970 12:00:00AM UTC, August 31 50144 10:33:20AM UTC
    

    应该是什么样子:

    751.95, 734.1, 745.45, 742.8, 1659987, 6358, 0, <time in IST zone>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   peak    6 年前

    只需使用括号:

     (.tickTimestamp | strftime("%B %d %Y %I:%M%p %Z"))
    

    如果需要相对于TZ的时间,请使用 strflocaltime ,但它需要比1.5更新版本的jq。