系统相关
首页 > 系统相关> > linux-如何突出显示文件中后续行之间的差异?

linux-如何突出显示文件中后续行之间的差异?

作者:互联网

我对大型日志文件分析做了很多紧急分析.通常,这需要记录日志并寻找更改.

我渴望有一个解决方案,可以突出显示这些更改,以使眼睛更容易跟踪.

我已经研究过工具,但似乎没有什么可以满足我的需求.我已经在Perl中编写了一些脚本,这些脚本可以大致做到这一点,但是我想要一个更完整的解决方案.

有人可以为此推荐工具吗?

解决方法:

为此,我编写了一个使用difflib.SequenceMatcher的Python脚本:

#!/usr/bin/python3

from difflib import SequenceMatcher
from itertools import tee
from sys import stdin

def pairwise(iterable):
    """s -> (s0,s1), (s1,s2), (s2, s3), ...

    https://docs.python.org/3/library/itertools.html#itertools-recipes
    """
    a, b = tee(iterable)
    next(b, None)
    return zip(a, b)

def color(c, s):
  """Wrap string s in color c.

  Based on https://stackoverflow.com/a/287944/1916449
  """
  try:
    lookup = {'r':'\033[91m', 'g':'\033[92m', 'b':'\033[1m'}
    return lookup[c] + str(s) + '\033[0m'
  except KeyError:
    return s

def diff(a, b):
  """Returns a list of paired and colored differences between a and b."""
  for tag, i, j, k, l in SequenceMatcher(None, a, b).get_opcodes():
    if tag == 'equal': yield 2 * [color('w', a[i:j])]
    if tag in ('delete', 'replace'): yield color('r', a[i:j]), ''
    if tag in ('insert', 'replace'): yield '', color('g', b[k:l])

if __name__ == '__main__':
  for a, b in pairwise(stdin):
    print(*map(''.join, zip(*diff(a, b))), sep='')

示例input.txt:

108  finished   /tmp/ts-out.5KS8bq   0       435.63/429.00/6.29 ./eval.exe -z 30
107  finished   /tmp/ts-out.z0tKmX   0       456.10/448.36/7.26 ./eval.exe -z 30
110  finished   /tmp/ts-out.wrYCrk   0       0.00/0.00/0.00 tail -n 1
111  finished   /tmp/ts-out.HALY18   0       460.65/456.02/4.47 ./eval.exe -z 30
112  finished   /tmp/ts-out.6hdkH5   0       292.26/272.98/19.12 ./eval.exe -z 1000
113  finished   /tmp/ts-out.eFBgoG   0       837.49/825.82/11.34 ./eval.exe -z 10

cat input.txt的输出| ./linediff.py:

linediff output

标签:logging,diff,linux,formatting
来源: https://codeday.me/bug/20191105/1997615.html