如何将日期格式的开始时间和以毫秒为单位的 运行 时间相加以在 unix 脚本中找到结束时间?
How to sum start time in date format and run time in milliseconds to find end time in unix script?
我有一个文件,其开始时间列的格式如下所示。
2019-10-30T08:04:30Z
2019-10-30T08:04:25Z
2019-10-30T08:04:30Z
我还有一个文件,其中包含 运行 以毫秒格式执行的作业时间。
2647ms
360ms
10440ms
。
.
.
如何在这些文件的两列中添加相应的行并在单独的文件中生成结束时间?
paste start_time.txt execution_time.txt | while IFS="$(printf '\t')" read -r f1 f2
do
# convert execution_time in seconds from ms
execution_time_ms="$(echo $f2 | cut -d'm' -f1)"
execution_time_ms="$(echo $execution_time_ms | cut -d's' -f1)"
execution_time_s=$(awk "BEGIN {printf \"%.10f\",$execution_time_ms/1000}")
# create dateformat string to add seconds
date_format=$f1
date_format+=" +$execution_time_s"
date_format+="seconds"
# create end date using dateformat string
end_time=`date --date="$date_format" +'%Y-%m-%d %H:%M:%S'`
end_time="${end_time/ /T}"
end_time+='Z'
# append end time
echo $end_time >> end_time.txt
done
这是一个可以解决您问题的脚本。
我已经按照你提到的那样使用了 3 个文件。
- start_time.txt(输入文件)
- execution_time.txt(输入文件)
- end_time.txt(输出文件)
我有一个文件,其开始时间列的格式如下所示。
2019-10-30T08:04:30Z
2019-10-30T08:04:25Z
2019-10-30T08:04:30Z
我还有一个文件,其中包含 运行 以毫秒格式执行的作业时间。
2647ms
360ms
10440ms
。 . .
如何在这些文件的两列中添加相应的行并在单独的文件中生成结束时间?
paste start_time.txt execution_time.txt | while IFS="$(printf '\t')" read -r f1 f2
do
# convert execution_time in seconds from ms
execution_time_ms="$(echo $f2 | cut -d'm' -f1)"
execution_time_ms="$(echo $execution_time_ms | cut -d's' -f1)"
execution_time_s=$(awk "BEGIN {printf \"%.10f\",$execution_time_ms/1000}")
# create dateformat string to add seconds
date_format=$f1
date_format+=" +$execution_time_s"
date_format+="seconds"
# create end date using dateformat string
end_time=`date --date="$date_format" +'%Y-%m-%d %H:%M:%S'`
end_time="${end_time/ /T}"
end_time+='Z'
# append end time
echo $end_time >> end_time.txt
done
这是一个可以解决您问题的脚本。 我已经按照你提到的那样使用了 3 个文件。
- start_time.txt(输入文件)
- execution_time.txt(输入文件)
- end_time.txt(输出文件)