Convert Epoch to date in log files

Ever had to go through a log file only to find timestamps are in epoch(unix) style format? Here’s a one liner that will convert them for you to review, without modifying the actual file:

cat mylogfile | perl -ne 'if (/(\d{10})/){ my $epoch= $1; $date=scalar(localtime($epoch)); s/$epoch/$date/g; print; } else { print; }'

It goes through all the input lines, finds a pattern of ten digits next to each other, and converts that to a date. The output could be much more nicer than this, but the one liner would be longer too. I like it because of its simplicity.

Post a Comment

Your email is never published nor shared. Required fields are marked *