Vit*_*lyB 81 diff command-line text
我想使用'diff'来获得字符差异和字符差异.例如,考虑:
档案1
abcde
abc
abcccd
Run Code Online (Sandbox Code Playgroud)
档案2
abcde
ab
abccc
Run Code Online (Sandbox Code Playgroud)
使用diff -u我得到:
@@ -1,3 +1,3 @@
abcde
-abc
-abcccd
\ No newline at end of file
+ab
+abccc
\ No newline at end of file
Run Code Online (Sandbox Code Playgroud)
但是,它只向我显示这些行的变化.我希望看到的是:
@@ -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
Run Code Online (Sandbox Code Playgroud)
你得到我的漂移.
现在,我知道我可以使用其他引擎来标记/检查特定线路上的差异.但我宁愿使用一种可以完成所有工作的工具.
小智 58
Git有一个单词diff,将所有字符定义为单词有效地为你提供了一个字符差异.但是,换行更改是IGNORED.
例:
创建一个这样的存储库:
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
Run Code Online (Sandbox Code Playgroud)
现在,做git diff --word-diff=color --word-diff-regex=. master^ master
,你会得到:
git diff http://oi60.tinypic.com/160wpb4.jpg
请注意如何在字符级别识别添加和删除,同时忽略新行的添加和删除.
你也可以尝试
git diff --word-diff=color --word-diff-regex=. master^ master
和
git diff --word-diff=color --word-diff-regex=. master^ master
zha*_*nxw 24
您可以使用:
diff -u f1 f2 |colordiff |diff-highlight
Run Code Online (Sandbox Code Playgroud)
colordiff
是一个Ubuntu包.你可以使用它安装它sudo apt-get install colordiff
.
diff-highlight
来自git(自2.9版本起).它位于/usr/share/doc/git/contrib/diff-highlight/diff-highlight
.你可以把它放在你的某个地方$PATH
.或者从差异化的项目中获取它.
Ned*_*Ned 18
如果你想以编程方式执行此操作,Python的difflib是ace.对于交互式使用,我使用vim的 diff模式(很容易使用:只需调用vim vimdiff a b
).我也偶尔使用Beyond Compare,它可以从diff工具中完成你所希望的一切.
我没有看到任何有用的命令行工具,但正如Will所说,difflib示例代码可能有所帮助.
Ven*_*oju 17
您可以cmp
在Solaris中使用该命令:
cmp
比较两个文件,如果它们不同,则告诉它们不同的第一个字节和行号.
Edu*_*scu 17
正如对主要答案的一条评论所说,您不必承诺使用 git diff:
git diff --no-index --word-diff=color --word-diff-regex=. file1 file2
Run Code Online (Sandbox Code Playgroud)
绿色将是第二个文件添加的字符。
红色将是第一个文件添加的字符。
Python有一个方便的库命名difflib
,可能有助于回答你的问题.
下面是两个difflib
用于不同python版本的oneliner .
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()))'
Run Code Online (Sandbox Code Playgroud)
这些可能会派上用场,因为它可以更容易随身携带.${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
Run Code Online (Sandbox Code Playgroud)
更可读的版本放在一个独立的文件中.
#!/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)
Run Code Online (Sandbox Code Playgroud)
彩色字符级 diff
输出
以下是您可以使用以下脚本和diff-highlight(这是 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
Run Code Online (Sandbox Code Playgroud)
(归功于@reracile对sed
突出显示的回答)
小智 5
ccdiff是完成该任务的便捷专用工具。这是您的示例的样子:
默认情况下,它突出显示颜色的差异,但它也可以在没有颜色支持的控制台中使用。
该软件包包含在Debian 的主存储库中:
ccdiff 是一种彩色差异,它也在更改的行内着色。
所有显示两个文件之间差异的命令行工具都无法显示出明显有用的微小变化。ccdiff 尝试提供
diff --color
或的外观和感觉colordiff
,但将彩色输出的显示从已删除和已添加的彩色行扩展到已更改行中已删除和已添加字符的颜色。