Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

awk, convering to unixtime

awk, convering to unixtime

awk, convering to unixtime


I have a log file in which first + last line are:

2015/11/26 10:12:39 [27741] building file list
2015/11/26 10:17:31 [27741] total size is 33199684413 speedup is 5914.76

I want to check how log rsync was running by subtracting dates from line 2 to 1 (converted to epoch).

my current solution is:
# sed -e 1b -e '$!d' logfile|awk '{print $1,$2}'|sed 'N;s/\n/ /'|while read s0 s1 e0 e1;do echo $(date -d "$e0 $e1" "+%s")-$(date -d "$s0 $s1" "+%s")|bc;done

can one awk command do it without extra commands and pipes?

RE: awk, convering to unixtime


Well, one GNU Awk command certainly can :

CODE --> line

master # awk '{e=mktime(gensub(/[/:]/," ","g",$1" "$2))}NR==1{s=e}END{print e-s}' logfile

Other Awk implementations which have no mktime() function would need to calculate themselves. So I would keep calling the external date command, just without the piping :

CODE --> line

master # awk '{e=$1" "$2}NR==1{s=e}END{print t(e)-t(s)}function t(d){("date -d \""d"\" \"+%s\"")|getline d;return d}' logfile


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close