在Linux Shell中,如何显示连续行中出现的日期差异?
我有如下输出:在Linux Shell中,如何显示连续行中出现的日期差异?,linux,bash,shell,awk,grep,Linux,Bash,Shell,Awk,Grep,我有如下输出: $ 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 a
$ 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
现在,我需要以秒为单位显示事务前后的时差(第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=$(日期-d“2012-10-12 11:48:30”+%s);t1=$(日期-d“2012-10-12 13:13:48”+%s);差异=$(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
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
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
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
我认为您最好编写一个python脚本来读取文件,对行进行排序,完成所有输出,最后在需要的地方添加一个datediff行 然而,我能想到的最简单的解决方案是在已有的基础上添加一个管道(“|”)并添加另一个awk,它为列1和列2的ate设置变量,用于在所有行之前和之后写入timediff行。类似于添加
| awk 'BEGIN{FS="]"}
{print $0}
/beforeEverything/{f=$1;sub(/[\[\-\:]/," ", f);f=mktime(f)}
/afterEverything/{a=$1;sub(/[\[\-\:]/," ", a);a=mktime(a);printf("Time for transaction in Seconds : %i", a-f)}'
还是什么?我认为您最好编写一个python脚本来读取文件、对行进行排序、执行所有输出,最后在需要的地方添加一个datediff行
awk -F '[\\[\\- ,:\\]]' 'function t(){return mktime($2" "$3" "$4" "$5" "$6" "$7)}NR%2{a=t()}!(NR%2){$0=$0"\nTime for transaction in Seconds : "t()-a}1' Filename
然而,我能想到的最简单的解决方案是在已有的基础上添加一个管道(“|”)并添加另一个awk,它为列1和列2的ate设置变量,用于在所有行之前和之后写入timediff行。类似于添加
| awk 'BEGIN{FS="]"}
{print $0}
/beforeEverything/{f=$1;sub(/[\[\-\:]/," ", f);f=mktime(f)}
/afterEverything/{a=$1;sub(/[\[\-\:]/," ", a);a=mktime(a);printf("Time for transaction in Seconds : %i", a-f)}'
还是什么?我认为您最好编写一个python脚本来读取文件、对行进行排序、执行所有输出,最后在需要的地方添加一个datediff行
awk -F '[\\[\\- ,:\\]]' 'function t(){return mktime($2" "$3" "$4" "$5" "$6" "$7)}NR%2{a=t()}!(NR%2){$0=$0"\nTime for transaction in Seconds : "t()-a}1' Filename
然而,我能想到的最简单的解决方案是在已有的基础上添加一个管道(“|”)并添加另一个awk,它为列1和列2的ate设置变量,用于在所有行之前和之后写入timediff行。类似于添加
| awk 'BEGIN{FS="]"}
{print $0}
/beforeEverything/{f=$1;sub(/[\[\-\:]/," ", f);f=mktime(f)}
/afterEverything/{a=$1;sub(/[\[\-\:]/," ", a);a=mktime(a);printf("Time for transaction in Seconds : %i", a-f)}'
还是什么?我认为您最好编写一个python脚本来读取文件、对行进行排序、执行所有输出,最后在需要的地方添加一个datediff行
awk -F '[\\[\\- ,:\\]]' 'function t(){return mktime($2" "$3" "$4" "$5" "$6" "$7)}NR%2{a=t()}!(NR%2){$0=$0"\nTime for transaction in Seconds : "t()-a}1' Filename
然而,我能想到的最简单的解决方案是在已有的基础上添加一个管道(“|”)并添加另一个awk,它为列1和列2的ate设置变量,用于在所有行之前和之后写入timediff行。类似于添加
| awk 'BEGIN{FS="]"}
{print $0}
/beforeEverything/{f=$1;sub(/[\[\-\:]/," ", f);f=mktime(f)}
/afterEverything/{a=$1;sub(/[\[\-\:]/," ", a);a=mktime(a);printf("Time for transaction in Seconds : %i", a-f)}'
还是什么?读的时候-r-l;
awk -F '[\\[\\- ,:\\]]' 'function t(){return mktime($2" "$3" "$4" "$5" "$6" "$7)}NR%2{a=t()}!(NR%2){$0=$0"\nTime for transaction in Seconds : "t()-a}1' Filename
做
echo$l;
DATE2=`echo$l|awk'{print$1”“$2}'| sed-r's/\[\\]\124;,.//g'`&&DIFF=$($(date-d“$DATE2”+%s)-(date-d“$DATE1”+%s));
如果[“${DIFF}”-lt'0'];然后
DATE1=${DATE2};
其他的
echo“以秒为单位的事务时间:${DIFF}”;
日期1='0';
差异='-1';
fi
完成<文件
读取时-r l;
做
echo$l;
DATE2=`echo$l|awk'{print$1”“$2}'| sed-r's/\[\\]\124;,.//g'`&&DIFF=$($(date-d“$DATE2”+%s)-(date-d“$DATE1”+%s));
如果[“${DIFF}”-lt'0'];然后
DATE1=${DATE2};
其他的
echo“以秒为单位的事务时间:${DIFF}”;
日期1='0';
差异='-1';
fi
完成<文件
读取时-r l;
做
echo$l;
DATE2=`echo$l|awk'{print$1”“$2}'| sed-r's/\[\\]\124;,.//g'`&&DIFF=$($(date-d“$DATE2”+%s)-(date-d“$DATE1”+%s));
如果[“${DIFF}”-lt'0'];然后
DATE1=${DATE2};
其他的
echo“以秒为单位的事务时间:${DIFF}”;
日期1='0';
差异='-1';
fi
完成<文件
读取时-r l;
做
echo$l;
DATE2=`echo$l|awk'{print$1”“$2}'| sed-r's/\[\\]\124;,.//g'`&&DIFF=$($(date-d“$DATE2”+%s)-(date-d“$DATE1”+%s));
如果[“${DIFF}”-lt'0'];然后
DATE1=${DATE2};
其他的
echo“以秒为单位的事务时间:${DIFF}”;
日期1='0';
差异='-1';
fi
完成<文件
你有(或能得到)GNU awk吗?你有(或能得到)GNU awk吗?你有(或能得到)GNU awk吗?你有(或能得到)GNU awk吗?谢谢格伦。非常感谢你在这里的帮助。这应该能满足我的要求。让我在脚本中使用它。感谢格伦,提供的解决方案效果良好。谢谢你。我需要在这里包括1个更多的更改,我需要打印、时间和用户ID以及事务时间。例如,当前输出为:[2015-10-28 12:38:05472][threadExecutor-1][INFO]com.delos:CustomerSaveHandler-afterEverthing UPDATE CUSTOMER-userId-mdmId kaallen:359510交易时间(秒):4.549。我需要o显示:交易时间(秒):4.445:kaallen:2015-10-28 11:33:05371。非常感谢你在这方面的帮助。谢谢Hanks Glenn非常感谢你的帮助这应该符合我的要求。让我在脚本中使用它。感谢格伦,提供的解决方案效果良好。谢谢你。我需要在这里包括1个更多的更改,我需要打印、时间和用户ID以及事务时间。例如,当前输出为:[2015-10-28 12:38:05472][threadExecutor-1][INFO]com.delos:CustomerSaveHandler-afterEverthing UPDATE CUSTOMER-userId-mdmId kaallen:359510交易时间(秒):4.549。我需要o显示:交易时间(秒):4.445:kaallen:2015-10-28 11:33:05371。非常感谢你在这方面的帮助。谢谢Hanks Glenn非常感谢你的帮助这应该符合我的要求。让我在脚本中使用它。感谢格伦,提供的解决方案效果良好。谢谢你。我需要在这里包括1个更多的更改,我需要打印、时间和用户ID以及事务时间。法罗群岛