在 linux 中找到 sequence/sorting 断开的行
find the lines where sequence/sorting is broken in linux
我有一个包含大量日志的日志文件,第一列包含纪元时间戳,理想情况下它们应该在 sequence/sorted 中。但是,如果系统出现问题,时间戳将重置为某个默认值,并且序列会中断并重新启动。我试图找到 sequence/sorting 变得乏味的行。请注意第一列中的序号。
例如:
101 aaa bbb ccc
102 aa dd ff gg
103 asd asd asdas
104 something goes wrong
101 restarting the time stamp
103 new start
104 going fine
105 smae here
102 ahh something unexpected
期望的输出:
104 something goes wrong
101 restarting the time stamp
105 smae here
102 ahh something unexpected
sort -c
有帮助,但我无法将其输出存储到任何文件以供进一步处理。如果有任何替代方法,请告诉我。
使用 awk:
awk ' < prev { print saved "\n" [=10=] "\n" } { prev = ; saved = [=10=] }' filename
这里就不多解释了,大家可以看到:
< prev { # if a line is out of order
print saved "\n" [=11=] "\n" # print it and the previous line
}
{ # and for all lines:
prev = # remember the things you need to determine
saved = [=11=] # when that is the case.
}
prev
最初是空的,被认为等于零,这对于大纪元时间戳应该没问题,除非你有 1970 年之前的日志条目。如果有,请将 < prev
替换为 NR > 1 && < prev
, 因为第一行不能乱序.
我有一个包含大量日志的日志文件,第一列包含纪元时间戳,理想情况下它们应该在 sequence/sorted 中。但是,如果系统出现问题,时间戳将重置为某个默认值,并且序列会中断并重新启动。我试图找到 sequence/sorting 变得乏味的行。请注意第一列中的序号。
例如:
101 aaa bbb ccc
102 aa dd ff gg
103 asd asd asdas
104 something goes wrong
101 restarting the time stamp
103 new start
104 going fine
105 smae here
102 ahh something unexpected
期望的输出:
104 something goes wrong
101 restarting the time stamp
105 smae here
102 ahh something unexpected
sort -c
有帮助,但我无法将其输出存储到任何文件以供进一步处理。如果有任何替代方法,请告诉我。
使用 awk:
awk ' < prev { print saved "\n" [=10=] "\n" } { prev = ; saved = [=10=] }' filename
这里就不多解释了,大家可以看到:
< prev { # if a line is out of order
print saved "\n" [=11=] "\n" # print it and the previous line
}
{ # and for all lines:
prev = # remember the things you need to determine
saved = [=11=] # when that is the case.
}
prev
最初是空的,被认为等于零,这对于大纪元时间戳应该没问题,除非你有 1970 年之前的日志条目。如果有,请将 < prev
替换为 NR > 1 && < prev
, 因为第一行不能乱序.