Scott
Scott

Reputation: 181

bash convert epoch time in tail of csv to human readable

I've spent a few hours trawling over dozens of StackOverflow questions, and they have gotten me closer to my answer, but not quite all the way.

I'm trying to combine several bash commands into one; to tail a csv log file, and convert the epoch dtstamp to human readable on the fly. So far I have worked through the following steps:

#1. Get just the columns I want from the log file:

tail -F config.log |awk -F, '{printf $1}{printf $3}{printf $4}{printf "   "}{print    $5}'
AW.BS._74.OD              3.000   0001630507243.820532   Local User
AW.BS._74.OnAction        3       0001630507243.820532   Local User
AW.BS._75.OfD             3.000   0001630507243.821193   Local User
AW.BS._75.OD              3.000   0001630507243.821193   Local User
AW.BS._75.OA              3       0001630507243.821193   Local User
AW.BS._76.OfD             3.000   0001630507243.821857   Local User
AW.BS._76.OD              3.000   0001630507243.821857   Local User
AW.BS._76.OA              3       0001630507243.821857   Local User
T.C._5.T.A._2.I                   0001630511595.829306   Local User
T.C._5.T.A._2.I                   0001630540127.214874   Local User

#2. Extract epoch on it's own

tail -F config.log |awk -F, '{print $4}'
0001630507243.820532
0001630507243.820532
0001630507243.821193
0001630507243.821193
0001630507243.821193
0001630507243.821857
0001630507243.821857
0001630507243.821857
0001630511595.829306
0001630540127.214874

#3. date doesn't like leading zeros or trailing decimals on an epoch time it's converting, so remove them:

tail config.log |awk -F, '{print $4}' |sed 's/^0*//' |sed 's/\.[^\.]*$//'

1630507243
1630507243
1630507243
1630507243
1630507243
1630507243
1630507243
1630507243
1630507243
1630511595

However - this step doesn't work if I'm using -F with tail. :(

#4 Test date conversion: works fine with a static epoch time:

date -d @1630540127
Wed Sep  1 23:48:47 GMT 2021

So, I can do each part on it's own, but I can't manage to combine them to see a live update of the log file showing columns 1, 3, <4 converted to readable> 5.

What have I missed?

Thanks for your help Gurus.

Upvotes: 0

Views: 297

Answers (1)

stack0114106
stack0114106

Reputation: 8711

Try this GNU awk

$ TZ=GMT awk '{ s=strftime("%a %b %d %T %Z %Y",$(NF-2));print $0,s }' scott.txt
AW.BS._74.OD              3.000   0001630507243.820532   Local User Wed Sep 01 14:40:43 GMT 2021
AW.BS._74.OnAction        3       0001630507243.820532   Local User Wed Sep 01 14:40:43 GMT 2021
AW.BS._75.OfD             3.000   0001630507243.821193   Local User Wed Sep 01 14:40:43 GMT 2021
AW.BS._75.OD              3.000   0001630507243.821193   Local User Wed Sep 01 14:40:43 GMT 2021
AW.BS._75.OA              3       0001630507243.821193   Local User Wed Sep 01 14:40:43 GMT 2021
AW.BS._76.OfD             3.000   0001630507243.821857   Local User Wed Sep 01 14:40:43 GMT 2021
AW.BS._76.OD              3.000   0001630507243.821857   Local User Wed Sep 01 14:40:43 GMT 2021
AW.BS._76.OA              3       0001630507243.821857   Local User Wed Sep 01 14:40:43 GMT 2021
T.C._5.T.A._2.I                   0001630511595.829306   Local User Wed Sep 01 15:53:15 GMT 2021
T.C._5.T.A._2.I                   0001630540127.214874   Local User Wed Sep 01 23:48:47 GMT 2021

Upvotes: 1

Related Questions