Unix Timestamp – Convert Unix Timestamp to Human-Readable Time

awkdatetext processingtimestamps

I would like to view a text file with awk/grep which contains a Unix timestamp in the first column.
How can I convert it to a human-readable time while viewing?

Best Answer

If the line starts with the unix timestamp, then this should do it:

perl -pe 's/^(\d+)/localtime $1/e' inputfilename

perl -p invokes a loop over the expression passed with -e which is executed for each line of the input, and prints the buffer at the end of the loop.

The expression uses the substition command s/// to match and capture a sequence of digits at the beginning of each line, and replace it with the local time representation of those digits interpreted as a unix timestamp. /e indicates that the replacement pattern is to be evaluated as an expression.

Related Question