Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Datetime 如何在Unix中减去两次以获得autosys作业的完成时间_Datetime_Unix_Autosys - Fatal编程技术网

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

Datetime 如何在Unix中减去两次以获得autosys作业的完成时间,datetime,unix,autosys,Datetime,Unix,Autosys,我要计算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 1

我要计算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
我写了下面的脚本来计算它:

输出:

$./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吗?如果你不这样做,这是一项艰苦的工作,但是如果你这样做了,虽然不容易,但是很容易,因为它支持时间操纵,请参见手册中的

文件中的日期格式没有帮助-甚至不清楚它是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($2, $3); t2 = cvt_mdy_hms($4, $5);
  printf "%-30s  %2s  time %8s;  [%10s %8s] = %10d  [%10s %8s] = %10d; delta = %6d\n",
         $1, $6, hms(t2 - t1), $2, $3, t1, $4, $5, 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。

你有没有过这样的工作:第一天23:04:59开始,第二天00:34:31结束?你的脚本效率太低了。您可以使用单个awk脚本处理数据。不,Jonathan,这些作业从IST时间早上6点开始,大部分在IST时间下午6点之前完成,并且这些作业使用重用文件“作业前一天”文件,因此不可能长时间运行。@Jonathan,我只是想知道,在unix中,除了冗长的子站expr$H2-$H1、expr$M2-$M1、expr$S2-$S等等,我们如何减去时间实体,就像我给你们展示的那样。将时间转换为参考时间午夜或“历元”的偏移量(以秒为单位)-1970-01-01 00:00:00+00:00。然后取这些偏移量之间的差值,并将其格式化为小时、分钟和秒。
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