【问题标题】:find the lines where sequence/sorting is broken in linux在linux中找到序列/排序被破坏的行
【发布时间】:2015-08-24 07:57:55
【问题描述】:

我有一个包含大量日志的日志文件,第一列包含纪元时间戳,理想情况下它们应该按顺序/排序。但是,如果系统时间戳出现问题,则会重置为某个默认值,并且序列会中断并重新启动。我正在尝试找到序列/排序变得不正常的行。请注意第一栏中的序列号。

例如:

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 有帮助,但我无法将其输出存储到任何文件中以供进一步处理。请让我知道是否有任何替代方法。

【问题讨论】:

    标签: linux sorting awk scripting


    【解决方案1】:

    使用 awk:

    awk '$1 < prev { print saved "\n" $0 "\n" } { prev = $1; saved = $0 }' filename
    

    这里就不多解释了,大家可以看到:

    $1 < prev {                    # if a line is out of order
      print saved "\n" $0 "\n"     # print it and the previous line
    }
    {                              # and for all lines:
      prev = $1                    # remember the things you need to determine
      saved = $0                   # when that is the case.
    }
    

    prev 最初为空并被视为等于 0,除非您有 1970 年之前的日志条目,否则对于 Epoch 时间戳应该没问题。如果这样做,请将 $1 &lt; prev 替换为 NR &gt; 1 &amp;&amp; $1 &lt; prev,因为第一行不能出问题了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-10
      • 2016-03-06
      • 2017-03-05
      • 2012-07-09
      • 1970-01-01
      • 1970-01-01
      • 2017-02-10
      相关资源
      最近更新 更多