Using 'diff' (or anything else) to get character-level diff between text files

前端 未结 15 2178
借酒劲吻你
借酒劲吻你 2020-12-02 05:40

I\'d like to use \'diff\' to get a both line difference between and character difference. For example, consider:

File 1

abcde
ab         


        
相关标签:
15条回答
  • 2020-12-02 06:10

    ccdiff is a convenient dedicated tool for the task. Here is what your example looks like with it:

    By default, it highlights the differences in color, but it can be used on a console without color support too.

    The package is included in the main repository of Debian:

    ccdiff is a colored diff that also colors inside changed lines.

    All command-line tools that show the difference between two files fall short in showing minor changes visuably useful. ccdiff tries to give the look and feel of diff --color or colordiff, but extending the display of colored output from colored deleted and added lines to colors for deleted and addedd characters within the changed lines.

    0 讨论(0)
  • 2020-12-02 06:11

    Here is an online text comparison tool: http://text-compare.com/

    It can highlight every single char that is different and continues compare the rest.

    0 讨论(0)
  • 2020-12-02 06:13

    You can use the cmp command in Solaris:

    cmp

    Compare two files, and if they differ, tells the first byte and line number where they differ.

    0 讨论(0)
  • 2020-12-02 06:14

    I also wrote my own script to solve this problem using the Longest common subsequence algorithm.

    It is executed as such

    JLDiff.py a.txt b.txt out.html

    The result is in html with red and green coloring. Larger files do exponentually take a longer amount of time to process but this does a true character by character comparison without checking line by line first.

    0 讨论(0)
  • 2020-12-02 06:16

    Python's difflib is ace if you want to do this programmatically. For interactive use, I use vim's diff mode (easy enough to use: just invoke vim with vimdiff a b). I also occaisionally use Beyond Compare, which does pretty much everything you could hope for from a diff tool.

    I haven't see any command line tool which does this usefully, but as Will notes, the difflib example code might help.

    0 讨论(0)
  • 2020-12-02 06:22

    Not a complete answer, but if cmp -l's output is not clear enough, you can use:

    sed 's/\(.\)/\1\n/g' file1 > file1.vertical
    sed 's/\(.\)/\1\n/g' file2 > file2.vertical
    diff file1.vertical file2.vertical
    
    0 讨论(0)
提交回复
热议问题