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

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

    Coloured, character-level diff ouput

    Here's what you can do with the the below script and diff-highlight (which is part of git):

    #!/bin/sh -eu
    
    # Use diff-highlight to show word-level differences
    
    diff -U3 --minimal "$@" |
      sed 's/^-/\x1b[1;31m-/;s/^+/\x1b[1;32m+/;s/^@/\x1b[1;34m@/;s/$/\x1b[0m/' |
      diff-highlight
    

    (Credit to @retracile's answer for the sed highlighting)

提交回复
热议问题