如何使用 awk 或 perl 脚本计算以下日志文件中的(刷新时间 - 聚合时间)的持续时间
09/03/2020 00:05:03.364 Aggregated 0 NMEs at a rate of 0 NMEs/sec
09/03/2020 00:05:03.366 Scheme S20_SessionClassAggregation tree contained 0 nmes, 0 flushed, 0 remain.
09/03/2020 00:05:03.582 Flushed 0 NMEs at a rate of 0 NMEs/sec
09/03/2020 00:20:03.598 Aggregated 0 NMEs at a rate of 0 NMEs/sec
09/03/2020 00:20:03.602 Scheme S20_SessionClassAggregation tree contained 0 nmes, 0 flushed, 0 remain.
09/03/2020 00:20:03.860 Flushed 0 NMEs at a rate of 0 NMEs/sec
例子:
我需要与第 3 行 (009/03/2020 00:05:03.582) - 第 1 行 (09/03/2020 00:05:03.364) 和第 6 行 (09/03/2020 00:20:03.860) - 的区别第 4 行 (09/03/2020 00:20:03.598)
预期成绩:
0 min 0 sec 218 ms
0 min 0 sec 262 ms
.
.
.
答案1
假设您的时间戳始终成对出现,您可以使用GNU sed
和来完成GNU coreutils
:
# Extract the relevant timestamps and convert them to secs + ns
<infile \
sed -nE 's/(.*) (Aggregated|Flushed).*/\1/; T; s/^/date -d "/; s/$/" +%s.%N/ep' |
# Find the time difference
sed '1~2 s/^/-/' |
paste -d+ - - |
bc |
# Print the difference in the desired format
while read dt; do
date -u -d "1970/01/01 + $(printf "%.3f" $dt) sec" +'%_M min %S sec %3N ms'
done
输出:
0 min 00 sec 218 ms
0 min 00 sec 262 ms