Using 'diff' (or anything else) to get cha

2019-01-16 11:09发布

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

File 1

abcde
abc
abcccd

File 2

abcde
ab
abccc

Using diff -u I get:

@@ -1,3 +1,3 @@
 abcde
-abc
-abcccd
\ No newline at end of file
+ab
+abccc
\ No newline at end of file

However, it only shows me that were changes in these lines. What I'd like to see is something like:

@@ -1,3 +1,3 @@
 abcde
-ab<ins>c</ins>
-abccc<ins>d</ins>
\ No newline at end of file
+ab
+abccc
\ No newline at end of file

You get my drift.

Now, I know I can use other engines to mark/check the difference on a specific line. But I'd rather use one tool that does all of it.

13条回答
趁早两清
2楼-- · 2019-01-16 11:17

Git has a word diff, and defining all characters as words effectively gives you a character diff. However, newline changes are IGNORED.

EXAMPLE:

Create a repository like this:

mkdir chardifftest
cd chardifftest
git init
echo -e 'foobarbaz\ncatdog\nfox' > file
git add -A; git commit -m 1
echo -e 'fuobArbas\ncat\ndogfox' > file
git add -A; git commit -m 2

Now, do git diff --word-diff=color --word-diff-regex=. master^ master and you'll get:

git diff http://oi60.tinypic.com/160wpb4.jpg

Note how both additions and deletions are recognized at the character level, while both additions and deletions of newlines are ignored.

You may also want to try
git diff --word-diff=plain --word-diff-regex=. master^ master
and
git diff --word-diff=porcelain --word-diff-regex=. master^ master

查看更多
Summer. ? 凉城
3楼-- · 2019-01-16 11:22

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.

查看更多
叛逆
4楼-- · 2019-01-16 11: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
查看更多
forever°为你锁心
5楼-- · 2019-01-16 11:27

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.

查看更多
别忘想泡老子
6楼-- · 2019-01-16 11:28

Coloured, character-level diff ouput

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

Coloured diff screenshot

#!/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)

查看更多
Lonely孤独者°
7楼-- · 2019-01-16 11:29

You can use:

diff -u f1 f2 |colordiff |diff-highlight

screenshot

colordiff is a Ubuntu package. You can install it using sudo apt-get install colordiff.

diff-highlight is from git (since version 2.9). It is located in /usr/share/doc/git/contrib/diff-highlight/diff-highlight. You can put it somewhere in your $PATH. Or get it from diff-so-fancy project.

查看更多
登录 后发表回答