Text-Processing
辨識日誌文件的持續時間
如何使用 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 . . .
假設您的時間戳總是成對出現,您可以使用
GNU sed
and來做到這一點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