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

我做了大量日志文件分析的紧急分析。 通常这将需要拖尾日志和寻找变化。

我渴望有一个解决scheme,将突出这些变化,使眼睛更容易跟踪。

我调查过的工具,似乎没有任何东西在那里,我正在寻找。 我已经用Perl编写了一些脚本,但是我想要一个更完整的解决scheme。

任何人都可以推荐一个工具吗?

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

 #!/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 http://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 cat input.txt | ./linediff.py

linediff输出

Levenshtein距离

维基百科:两个字符串之间的Levenshtein距离是将一个字符串转换为另一个字符串所需的最少操作次数,其中操作是插入,删除或替换单个字符。

 public static int LevenshteinDistance(char[] s1, char[] s2) { int s1p = s1.length, s2p = s2.length; int[][] num = new int[s1p + 1][s2p + 1]; // fill arrays for (int i = 0; i <= s1p; i++) num[i][0] = i; for (int i = 0; i <= s2p; i++) num[0][i] = i; for (int i = 1; i <= s1p; i++) for (int j = 1; j <= s2p; j++) num[i][j] = Math.min(Math.min(num[i - 1][j] + 1, num[i][j - 1] + 1), num[i - 1][j - 1] + (s1[i - 1] == s2[j - 1] ? 0 : 1)); return num[s1p][s2p]; } 

Java中的示例应用程序

字符串差异

替代文字

应用程序使用LCS算法将2个文本输入连接成1.结果将包含最少的指令集,以便为另一个字符串创建一个字符串。 显示指令下面的连接文本。

下载应用程序: String Diff.jar

下载源码: Diff.java

http://neil.fraser.name/software/diff_match_patch/svn/trunk/demos/demo_diff.html

..这个看起来很有希望,当我玩了更多的时候会更新这个信息。