Search code examples
bashtimestampunix-timestampjq

Apply strftime on last column in jq


I am processing a file and filtering out certain fields from it. Then, I am converting the whole result to a CSV file. However, the last column that I am filtering out is the epoch timestamp (upto millisecond).

Searching around, I found that I can use the strftime function in jq for conversion, however; when applying the | strftime inside the whole array, it applies the function to all the values.

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(", ")'

Can I modify this so that strftime("%B %d %Y %I:%M%p %Z") only applies to the .tickTimestmap value? If not, Would I have to depend on awk?


Log lines from 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}]}}

What I am currently generating:

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

What it should be like:

751.95, 734.1, 745.45, 742.8, 1659987, 6358, 0, <time in IST zone>

Solution

  • Simply use parentheses:

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

    If you want the time relative to TZ, use strflocaltime, which however requires a more recent version of jq than 1.5.