Let us consider a file where the
1st column is the Process name,
2nd is the start time of the process, and
3rd column is the end time of the process.
The requirement is to find the time consumed by the process which is the difference between the start and the end times.
1. File in which the date and time component are separated by a space:
$ cat file P1,2012 12 4 21 36 48,2012 12 4 22 26 53 P2,2012 12 4 20 36 48,2012 12 4 21 21 23 P3,2012 12 4 18 36 48,2012 12 4 20 12 35Time difference in seconds:
$ awk -F, '{d2=mktime($3);d1=mktime($2);print $1","d2-d1,"secs";}' file
P1,3005 secs
P2,2675 secs
P3,5747 secs
Using mktime function, the Unix time is calculated for the date time strings, and their difference gives us the time elapsed in seconds.