在 jq 的最后一列上应用 strftime

Apply strftime on last column in jq

我正在处理一个文件并从中过滤掉某些字段。然后,我将整个结果转换为 CSV 文件。但是,我过滤掉的最后一列是纪元时间戳(最多毫秒)。

四处寻找,我发现我可以使用jq中的strftime函数进行转换,但是;在整个数组中应用 | 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>

只需使用括号:

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

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