Search code examples
bashunixunix-timestamp

bash Convert unixtimestamp in file


i want to convert an unixtimestamp in a file. I used a bash script, that loads an file from the internet. The data in the file looks like that:

line 09 to Allstrom 1486568580 is ok
line 19 to rom      1486568450 is ok

How is it possible to convert this unixtimestamp to an human date ? I know i must use date -ud timestamp but how can i get the timestamp in my bash script ?

thanks for helping me

Markus


Solution

  • You can make an Awk to call the date -d on the EPOCH value, convert to the timestamp format required and print rest of the line,

    awk '{ cmd="date -d@"$5" +'%H:%M:%S'"; cmd | getline mydate; close(cmd); $5=mydate}1' file
    line 09 to Allstrom 21:13:00 is ok
    line 19 to rom 21:10:50 is ok
    

    The cmd | getline syntax in awk allows you to catch the result of date in an awk variable mydate for printing it later.

    (or) using the native gawk function strftime on column 5 containing the EPOCH value,

    awk '{$5=strftime("%H:%M:%S", $5)} 1' file
    line 09 to Allstrom 21:13:00 is ok
    line 19 to rom 21:10:50 is ok