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

前端 未结 15 2271
借酒劲吻你
借酒劲吻你 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:36

    Python has convenient library named difflib which might help answer your question.

    Below are two oneliners using difflib for different python versions.

    python3 -c 'import difflib, sys; \
      print("".join( \
        difflib.ndiff( \ 
          open(sys.argv[1]).readlines(),open(sys.argv[2]).readlines())))'
    python2 -c 'import difflib, sys; \
      print "".join( \
        difflib.ndiff( \
          open(sys.argv[1]).readlines(), open(sys.argv[2]).readlines()))'
    

    These might come in handy as a shell alias which is easier to move around with your .${SHELL_NAME}rc.

    $ alias char_diff="python2 -c 'import difflib, sys; print \"\".join(difflib.ndiff(open(sys.argv[1]).readlines(), open(sys.argv[2]).readlines()))'"
    $ char_diff old_file new_file
    

    And more readable version to put in a standalone file.

    #!/usr/bin/env python2
    from __future__ import with_statement
    
    import difflib
    import sys
    
    with open(sys.argv[1]) as old_f, open(sys.argv[2]) as new_f:
        old_lines, new_lines = old_f.readlines(), new_f.readlines()
    diff = difflib.ndiff(old_lines, new_lines)
    print ''.join(diff)
    

提交回复
热议问题