我想计算一个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 $1}' rough | sed -n "$i p"`
START_DATE=`awk -F' ' '{print $2}' rough | sed -n "$i p"`
END_DATE=`awk -F' ' '{print $4}' rough | sed -n "$i p"`
START_TIME=`awk -F' ' '{print $3}' rough | sed -n "$i p"`
END_TIME=`awk -F' ' '{print $5}' 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>>第一份工作
答案 0 :(得分:1)
你在机器上有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($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中完成。
答案 1 :(得分:0)
我使用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