找到linux中日志文件中两行日期的区别

时间:2016-08-29 12:29:45

标签: linux unix

我有一个包含以下格式数据的文件:

2016-08-26 14:43:00,840[http-nio-8014-exec-2]INFO
2016-08-26 14:43:00,877[http-nio-8014-exec-2]INFO
2016-08-26 14:43:01,130[http-nio-8014-exec-9]INFO
2016-08-26 14:43:01,171[http-nio-8014-exec-9]INFO
2016-08-26 14:43:01,484[http-nio-8014-exec-8]INFO
2016-08-26 14:43:01,523[http-nio-8014-exec-8]INFO
2016-08-26 14:43:02,091[http-nio-8014-exec-1]INFO
2016-08-26 14:43:02,132[http-nio-8014-exec-1]INFO

我想计算第2行和第1行,第4行和第3行之间的日期差异,依此类推。并希望下面的输出行

Difference between line 2 and 1 is 37 ms
Difference between line 4 and 3 is 41 ms
Difference between line 6 and 5 is 39 ms

需要帮助。

3 个答案:

答案 0 :(得分:1)

尝试以下

python sample.py sample.log

<强> sameple.py

import fileinput
from datetime import datetime
import time, datetime, re

dt_pat = re.compile(r"(\d+)\-(\d+)-(\d+)\s+(\d+):(\d+):(\d+),(\d+)")
def mstime( tstr ):
    m = dt_pat.match( tstr )
    if m==None: return m
    v = [int(s) for s in m.groups()]
    return datetime.datetime(*v)

content = []
for line in fileinput.input():
    content.append(line.strip().split('[')[0])

for time in range(0,len(content)-1):
     diff = mstime( content[time+1])-mstime( content[time])
     print "{:f}".format(float(diff.total_seconds()))

fileinput.close()

答案 1 :(得分:1)

如果你想在标记问题时坚持POSIX shell,你可以做类似以下的事情:

#!/bin/sh

declare -i cnt=0
last=""
while read -r line; do
    ((cnt == 0)) && { ((cnt++)); last="$line"; continue; }

    tline="${line%%[*}"
    tline="${tline##*,}"
    dindex=$(expr index "$tline" [1-9])
    ((dindex > 0)) && tline=$(expr substr "$tline" "$dindex" 3)

    tlast="${last%%[*}"
    tlast="${tlast##*,}"
    dindex=$(expr index "$tlast" [1-9])
    ((dindex > 0)) && tlast=$(expr substr "$tlast" "$dindex" 3)

    tmdiff=$((tline - tlast))
    ((tmdiff < 0)) && ((tmdiff+=1000))

    printf "Difference between line %d and %d is %s ms\n" \
    "$((cnt + 1))" "$cnt" $tmdiff

    last="$line"
    ((cnt++))
done < "$1"

注意:以上假设条目之间的差异小于1秒,某些版本的read不支持-r选项(只需删除它)是这样的))

示例使用/输出

只需将日志文件名作为脚本的第一个参数传递:

$ sh logtmdiff.sh dat/logtimes.txt
Difference between line 2 and 1 is 37 ms
Difference between line 3 and 2 is 253 ms
Difference between line 4 and 3 is 41 ms
Difference between line 5 and 4 is 313 ms
Difference between line 6 and 5 is 39 ms
Difference between line 7 and 6 is 568 ms
Difference between line 8 and 7 is 41 ms

答案 2 :(得分:0)

感谢Ravichandra, 这个脚本通过一些小改动为我工作,因为我想要第2行和第2行之间的差异。 1然后4&amp; 3 ...因此迭代器索引时间必须增加2。

import fileinput
from datetime import datetime
import time, datetime, re

dt_pat = re.compile(r"(\d+)\-(\d+)-(\d+)\s+(\d+):(\d+):(\d+),(\d+)")
def mstime( tstr ):
    m = dt_pat.match( tstr )
    if m==None: return m
    v = [int(s) for s in m.groups()]
    return datetime.datetime(*v)

content = []
for line in fileinput.input():
    content.append(line.strip().split('[')[0])

for time in range(0,len(content)-1,2):
     diff = mstime( content[time+1])-mstime( content[time])
     print "{:f}".format(float(diff.total_seconds()))

fileinput.close()