使用“diff"(或其他任何东西)来获取文本文件之间的字符级差异 [英] Using 'diff' (or anything else) to get character-level diff between text files

查看:19
本文介绍了使用“diff"(或其他任何东西)来获取文本文件之间的字符级差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 'diff' 来获取行差异和字符差异.例如,考虑:

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

文件 1

abcde
abc
abcccd

文件 2

abcde
ab
abccc

使用 diff -u 我得到:

@@ -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

你明白我的意思了.

现在,我知道我可以使用其他引擎来标记/检查特定行上的差异.但我更愿意使用一种工具来完成所有工作.

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.

推荐答案

Git 有一个词差异,有效地将所有字符定义为词会给你一个字符差异.但是,换行符更改被忽略.

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

像这样创建一个存储库:

Create a repository like this:

mkdir chardifftest
cd chardifftest
git init
echo -e 'foobarbaz
catdog
fox' > file
git add -A; git commit -m 1
echo -e 'fuobArbas
cat
dogfox' > file
git add -A; git commit -m 2

现在,执行 git diff --word-diff=color --word-diff-regex=.master^ master 你会得到:

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

注意如何在字符级别识别添加和删除,而忽略换行符的添加和删除.

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 one of these:

git diff --word-diff=plain --word-diff-regex=. master^ master
git diff --word-diff=porcelain --word-diff-regex=. master^ master

这篇关于使用“diff"(或其他任何东西)来获取文本文件之间的字符级差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆