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