欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

用Linux shell 计算两个时间差

在日志中有两条记录
id 开 始 时 间 结 束 时 间
000485e7db1236cfc0d91f29c555af5e 2011-11-28 15:55:37 2011-11-28 15:55:52 
00049dc21b0a7dca98548c875354f77f 2011-11-28 16:21:01 2011-11-28 16:21:23 
如何用linux shell 命令计算出:(结束时间 - 开始时间)的秒数?




start=`date +%s -d "2011-11-28 15:55:37"`
end=`date +%s -d "2011-11-28 15:55:52"`
echo $(($end-$start))

这个前提是你能拿到开始和结束时间字串的基础上的。

如果你的日志文件输出格式是上述的话,还有个方法:
cat 日志文件 | awk '/^000/{
start=$2 " " $3
end=$4 " " $5

stm=mktime(gensub(/[:-]/, " ", "g", start))
etm=mktime(gensub(/[:-]/, " ", "g", end))

print $1 " - " etm-stm
}'


来自  https://zhidao.baidu.com/question/348630182.html

普通分类: