如何在 Unix 中减去两次以获得 autosys 作业的完成时间

How to subtract two times in Unix to get the completion time for autosys job

我想计算 autosys 作业的完成时间:

我在文件中有以下工作状态:

$cat rough
abc_why_the_infra_dnjob               10/05/2017 10:41:36  10/05/2017 12:52:02  SU
abc_why_the_avloqhk_dnjob             10/05/2017 09:53:36  10/05/2017 10:33:03  SU
abc_why_the_avlogsg_dnjob             10/05/2017 10:33:14  10/05/2017 10:41:01  SU
abc_why_the_avalokin_dnjob            10/05/2017 09:37:36  10/05/2017 09:53:06  SU
abc_why_the_mastercard_dnjob          10/05/2017 13:29:36  10/05/2017 14:21:02  SU
abc_why_the_tcs_dnjob                 10/05/2017 03:13:36  10/05/2017 03:22:02  SU
abc_why_the_cogni_dnjob               10/05/2017 09:20:36  10/05/2017 09:37:02  SU
abc_why_the_dnjob                  10/05/2017 03:41:36  10/05/2017 04:08:02  SU

我写了下面的脚本来计算它:


$ cat sod.sh

#!/bin/bash

number=`cat rough| wc -l`

for i in `seq 1 $number`
do

        job_name=`awk -F' ' '{print }' rough | sed -n "$i p"`

        START_DATE=`awk -F' ' '{print }' rough | sed -n "$i p"`
        END_DATE=`awk -F' ' '{print }' rough | sed -n "$i p"`

        START_TIME=`awk -F' ' '{print }' rough | sed -n "$i p"`
        END_TIME=`awk -F' ' '{print }' rough | sed -n "$i p"`

        if [[ $START_DATE == $END_DATE ]]
        then
                T1=`date --date="${END_DATE} ${END_TIME}" "+%s"`
                T2=`date --date="${START_DATE} ${START_TIME}" "+%s"`

                TIME_DIFFERENCE=`expr $T2 - $T1`
                COMPLITION_TIME=`date -d "@${TIME_DIFFERENCE}" "+%H:%M:%S"`

                echo $COMPLITION_TIME
        fi
done

输出:

$./sod.sh
03:19:34
04:50:33
05:22:13
05:14:30
04:38:34
05:21:34
05:13:34
05:03:34

我现在的查询:输出不正确你能建议我吗? 我期待正确的答案,例如: 02:11 >> 第一份工作

我使用 Python 脚本得到了答案:

>>> s1='10:41:36'
>>> s2='12:52:02'
>>> import datetime
>>> import time
>>> total_time=(datetime.datetime.strptime(s2,'%H:%M:%S') - datetime.datetime.strptime(s1,'%H:%M:%S'))
>>> print total_time
2:10:26

你的机器上有 GNU Awk 吗?如果你不这样做会很困难——但如果你这样做会更容易(虽然不容易),因为它支持时间操作——请参阅手册中的 time functions

文件中的日期格式没有帮助——甚至不清楚它是 mm/dd/yyyy 还是 dd/mm/yyyy 格式,尽管问题是在 2017-10- 05,很有可能是mm/dd/yyyy.

这个使用 gawk 的脚本(GNU Awk — 您也许可以只使用 awk)完成这项工作。它使用两个用户定义函数和mktime()。它不使用 strftime() 因为它在当地时间工作,这对我来说不正确,对你来说可能也不正确(但否则会很想使用 strtime("%H:%M:%S", t2 - t1) 来获取时间值hh:mm:ss 单位)。

gawk '
function cvt_mdy_hms(d0, t0,    d1, t1, dt, rv){ #d0="10/05/2017"; t0="10:41:36";
    split(d0, d1, "/"); split(t0, t1, ":");
    dt = d1[3] " " d1[1] " " d1[2] " " t1[1] " " t1[2] " " t1[3];
    rv = mktime(dt);
    # print "[", dt, "] =", rv;
    return rv
}
function hms(secs,    hh, mm, ss, rv) {
    hh = int(secs / 3600);
    mm = int(secs / 60) % 60;
    ss = secs % 60;
    rv = sprintf("%.2d:%.2d:%.2d", hh, mm, ss);
    # print "[", secs, " = ", rv, "]";
    return rv;
}
NF == 6 { t1 = cvt_mdy_hms(, ); t2 = cvt_mdy_hms(, );
  printf "%-30s  %2s  time %8s;  [%10s %8s] = %10d  [%10s %8s] = %10d; delta = %6d\n",
         , , hms(t2 - t1), , , t1, , , t2, t2 - t1;
}' data

给定您的数据文件(在名为 data 的文件中),输出为:

abc_why_the_infra_dnjob         SU  time 02:10:26;  [10/05/2017 10:41:36] = 1507225296  [10/05/2017 12:52:02] = 1507233122; delta =   7826
abc_why_the_avloqhk_dnjob       SU  time 00:39:27;  [10/05/2017 09:53:36] = 1507222416  [10/05/2017 10:33:03] = 1507224783; delta =   2367
abc_why_the_avlogsg_dnjob       SU  time 00:07:47;  [10/05/2017 10:33:14] = 1507224794  [10/05/2017 10:41:01] = 1507225261; delta =    467
abc_why_the_avalokin_dnjob      SU  time 00:15:30;  [10/05/2017 09:37:36] = 1507221456  [10/05/2017 09:53:06] = 1507222386; delta =    930
abc_why_the_mastercard_dnjob    SU  time 00:51:26;  [10/05/2017 13:29:36] = 1507235376  [10/05/2017 14:21:02] = 1507238462; delta =   3086
abc_why_the_tcs_dnjob           SU  time 00:08:26;  [10/05/2017 03:13:36] = 1507198416  [10/05/2017 03:22:02] = 1507198922; delta =    506
abc_why_the_cogni_dnjob         SU  time 00:16:26;  [10/05/2017 09:20:36] = 1507220436  [10/05/2017 09:37:02] = 1507221422; delta =    986
abc_why_the_dnjob               SU  time 00:26:26;  [10/05/2017 03:41:36] = 1507200096  [10/05/2017 04:08:02] = 1507201682; delta =   1586

也可以在 Perl 中轻松完成这项工作,在纯 Python 中也是如此。