diff 仅显示行内的差异

ano*_*nol 9 diff shell-script

我想比较两个文件中的行,但为了尽量减少输出中的噪音,我只想打印行中的实际差异。

例如,给定以下两个文件:

一个.txt

a b c d e f g h i j k l m n o p q r s t u v w x y z
Run Code Online (Sandbox Code Playgroud)

b.txt

a B c d e f g h i j k l m n o p q r s t u v w x y z
Run Code Online (Sandbox Code Playgroud)

(它们之间的区别是字母的大小写b

我希望输出类似于:

[-b-]{+B+}
Run Code Online (Sandbox Code Playgroud)

目前,我发现的最佳方法是使用git diff --word-diff,但它输出整行:

a [-b-]{+B+} c d e f g h i j k l m n o p q r s t u v w x y z
Run Code Online (Sandbox Code Playgroud)

除了手动解析输出之外,还有更直接的方法吗?此外,理想情况下,我更喜欢使用比 更常用的东西git diff,例如不需要用户安装额外软件包的 POSIX shell 工具。

Kus*_*nda 14

使用wdiff

$ wdiff -3 a.txt b.txt

======================================================================
 [-b-] {+B+}
======================================================================
Run Code Online (Sandbox Code Playgroud)

-3---no-common选项将删除的两个文件之间共同的和只显示不同的话。

===...横幅广告(和空行)可与被移除grep

$ wdiff -3 a.txt b.txt | grep -vx '=*'
 [-b-] {+B+}
Run Code Online (Sandbox Code Playgroud)

wdiffdiff如果您给它-d--diff-input选项,也可以读取统一数据,例如从git

git diff somefile | wdiff -d -3
Run Code Online (Sandbox Code Playgroud)

虽然wdiff不是 POSIX 工具,但它通常可用。