0

I am on a ubuntu machine trying to read from log files and found the logs contain date and time in epoch format(seconds). How can I convert these dates in numbers into any readable format from command line?

1411622206, HOST ALERT, host-001,DOWN,SOFT,1,CHECK_NRPE, Socket timeout after 10 seconds.
1411622586, HOST ALERT, host-001,DOWN,SOFT,1,CHECK_NRPE, Socket timeout after 10 seconds.
1411623976, HOST ALERT, host-021,DOWN,SOFT,1,CHECK_NRPE, Socket timeout after 10 seconds.
1411624986, HOST ALERT, host-055,DOWN,SOFT,1,CHECK_NRPE, Socket timeout after 10 seconds.
1411625076, HOST ALERT, host-023,DOWN,SOFT,1,CHECK_NRPE, Socket timeout after 10 seconds.
1411625356, HOST ALERT, host-032,DOWN,SOFT,1,CHECK_NRPE, Socket timeout after 10 seconds.
1411625736, HOST ALERT, host-044,DOWN,SOFT,1,CHECK_NRPE, Socket timeout after 10 seconds.
1
  • 2
    Thanks all! perl made my job easier: perl -pe 's/(\d+)/localtime($1)/e' /path/file.log | less > /path/newfile.log Sep 25, 2014 at 13:29

2 Answers 2

1

What about using date -d@SECONDS_SINCE_1970 [some format]?

while IFS=, read -r f1 f2
do
    echo "$(date -d@$f1),$f2"
done < file

Then you can play with the date options, so that you can get for example:

$ date -d@1411622586 "+%D %T"
09/25/14 07:23:06

Test

$ while IFS=, read -r f1 f2; do echo "$(date -d@$f1),$f2"; done < file
Thu Sep 25 07:16:46 CEST 2014, HOST ALERT, host-001,DOWN,SOFT,1,CHECK_NRPE, Socket timeout after 10 seconds.
Thu Sep 25 07:23:06 CEST 2014, HOST ALERT, host-001,DOWN,SOFT,1,CHECK_NRPE, Socket timeout after 10 seconds.
Thu Sep 25 07:46:16 CEST 2014, HOST ALERT, host-021,DOWN,SOFT,1,CHECK_NRPE, Socket timeout after 10 seconds.
Thu Sep 25 08:03:06 CEST 2014, HOST ALERT, host-055,DOWN,SOFT,1,CHECK_NRPE, Socket timeout after 10 seconds.
Thu Sep 25 08:04:36 CEST 2014, HOST ALERT, host-023,DOWN,SOFT,1,CHECK_NRPE, Socket timeout after 10 seconds.
Thu Sep 25 08:09:16 CEST 2014, HOST ALERT, host-032,DOWN,SOFT,1,CHECK_NRPE, Socket timeout after 10 seconds.
Thu Sep 25 08:15:36 CEST 2014, HOST ALERT, host-044,DOWN,SOFT,1,CHECK_NRPE, Socket timeout after 10 seconds.
0

From the date manual (info date):

To convert such an unwieldy number of seconds back to a more readable form, use a command like this:

# local time zone used
date -d '1970-01-01 UTC 946684800 seconds' +"%Y-%m-%d %T %z"
1999-12-31 19:00:00 -0500

Or if you do not mind depending on the @ feature present since coreutils 5.3.0, you could shorten this to:

date -d @946684800 +"%F %T %z"
1999-12-31 19:00:00 -0500

Often it is better to output UTC-relative date and time:

date -u -d '1970-01-01 946684800 seconds' +"%Y-%m-%d %T %z"
2000-01-01 00:00:00 +0000

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .