Search code examples
timeawkisoepoch

Awk ISO to epoch


I am writing a script in bash using awk (mawk) and I would like to return the output of a system command to awk.

I have a variable called created that is in ISO format: 2013-12-26T17:03:05Z and I want to convert it from ISO to epoch. I can run this line created_epoch=system("date +\%s -d " created) and it prints the output to the terminal, but the variable created_epoch is equal to 0, not the epoch time.

Does anyone know how I can convert an ISO format to epoch in awk?

Thanks!


Solution

  • Try using getline into a variable from a pipe instead of system

    cmd="date +\%s -d "created; cmd | getline current_time
    

    https://www.gnu.org/software/gawk/manual/html_node/Getline_002fVariable_002fPipe.html#Getline_002fVariable_002fPipe

    The system command returns the exit status returned by the command that was executed as its value. http://www.delorie.com/gnu/docs/gawk/gawk_137.html