gpt4 book ai didi

linux - 如何在 Linux Shell 中显示出现在连续行中的日期差异?

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:08:44 30 4
gpt4 key购买 nike

我有这样的输出:

$ cat newdata1| awk 'BEGIN { FS = "+" } ; { print $2"~"$1"+"$2 }'|sort -k1,2|cut -d "~" -f2
[2015-09-11 14:42:18,053] [threadExecutor-7] [INFO ] com.delos: CustomerSaveHandler - beforeEverything NEW_CUSTOMER - userId - mdmId - InteractionId are + eneblett:0null:null
[2015-09-11 14:42:57,655] [threadExecutor-8] [INFO ] com.delos: CustomerSaveHandler - afterEverthing NEW_CUSTOMER - userId - mdmId - InteractionId are + eneblett:835808 :null
[2015-09-11 14:46:03,264] [threadExecutor-5] [INFO ] com.delos: CustomerSaveHandler - beforeEverything NEW_CUSTOMER - userId - mdmId - InteractionId are + tahicks:0null:null
[2015-09-11 14:46:40,407] [threadExecutor-5] [INFO ] com.delos: CustomerSaveHandler - afterEverthing NEW_CUSTOMER - userId - mdmId - InteractionId are + tahicks:835811 :null
[2015-09-11 14:40:03,264] [threadExecutor-5] [INFO ] com.delos: CustomerSaveHandler - beforeEverything NEW_CUSTOMER - userId - mdmId - InteractionId are + zz:0null:null
[2015-09-11 14:40:57,655] [threadExecutor-8] [INFO ] com.delos: CustomerSaveHandler - afterEverthing NEW_CUSTOMER - userId - mdmId - InteractionId are + zz:835800 :null

现在,我需要在 beforeEverything 和 afterEverthing 事务之后(第 1-2、3-4、5-6 行等)以秒为单位显示时差:

所需输出示例:

[2015-09-11 14:42:18,053] [threadExecutor-7] [INFO ] com.delos: CustomerSaveHandler - beforeEverything NEW_CUSTOMER - userId - mdmId - InteractionId are + eneblett:0null:null
[2015-09-11 14:42:57,655] [threadExecutor-8] [INFO ] com.delos: CustomerSaveHandler - afterEverthing NEW_CUSTOMER - userId - mdmId - InteractionId are + eneblett:835808 :null
Time for transaction in Seconds : 39
[2015-09-11 14:46:03,264] [threadExecutor-5] [INFO ] com.delos: CustomerSaveHandler - beforeEverything NEW_CUSTOMER - userId - mdmId - InteractionId are + tahicks:0null:null
[2015-09-11 14:46:40,407] [threadExecutor-5] [INFO ] com.delos: CustomerSaveHandler - afterEverthing NEW_CUSTOMER - userId - mdmId - InteractionId are + tahicks:835811 :null
Time for transaction in Seconds : 37
[2015-09-11 14:40:03,264] [threadExecutor-5] [INFO ] com.delos: CustomerSaveHandler - beforeEverything NEW_CUSTOMER - userId - mdmId - InteractionId are + zz:0null:null
[2015-09-11 14:40:57,655] [threadExecutor-8] [INFO ] com.delos: CustomerSaveHandler - afterEverthing NEW_CUSTOMER - userId - mdmId - InteractionId are + zz:835800 :null
Time for transaction in Seconds : 54
...

不需要毫秒级的精度

也许我们可以使用一些日期操作,例如:t=$(date -d "2012-10-12 11:48:30"+%s);t1=$(date -d "2012-10-12 13:13:48"+%s);diff=$(expr $t1 - $t);echo $diff但我不知道如何将它与之前的命令一起使用。

提前致谢。

最佳答案

GNU awk 具有内置时间函数:

gawk -F'[][]' '
function totime(datestring, a,time) {
split(datestring, a, ",")
return(mktime(gensub(/[-:]/, " ", "g", a[1])) + a[2]/1000)
}
{print}
/beforeEverything/ {startTime = totime($2)}
/afterEverthing/ {print "Time for transaction in seconds: " (totime($2) - startTime)}
# .......^^ note typo in your log.
' log
[2015-09-11 14:42:18,053] [threadExecutor-7] [INFO ] com.delos: CustomerSaveHandler - beforeEverything NEW_CUSTOMER - userId - mdmId - InteractionId are + eneblett:0null:null
[2015-09-11 14:42:57,655] [threadExecutor-8] [INFO ] com.delos: CustomerSaveHandler - afterEverthing NEW_CUSTOMER - userId - mdmId - InteractionId are + eneblett:835808 :null
Time for transaction in seconds: 39.602
[2015-09-11 14:46:03,264] [threadExecutor-5] [INFO ] com.delos: CustomerSaveHandler - beforeEverything NEW_CUSTOMER - userId - mdmId - InteractionId are + tahicks:0null:null
[2015-09-11 14:46:40,407] [threadExecutor-5] [INFO ] com.delos: CustomerSaveHandler - afterEverthing NEW_CUSTOMER - userId - mdmId - InteractionId are + tahicks:835811 :null
Time for transaction in seconds: 37.143
[2015-09-11 14:40:03,264] [threadExecutor-5] [INFO ] com.delos: CustomerSaveHandler - beforeEverything NEW_CUSTOMER - userId - mdmId - InteractionId are + zz:0null:null
[2015-09-11 14:40:57,655] [threadExecutor-8] [INFO ] com.delos: CustomerSaveHandler - afterEverthing NEW_CUSTOMER - userId - mdmId - InteractionId are + zz:835800 :null
Time for transaction in seconds: 54.391

关于linux - 如何在 Linux Shell 中显示出现在连续行中的日期差异?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32532464/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com