ログファイルの期間の識別

ログファイルの期間の識別

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

おすすめ記事