如何颜色差异输出?

Ama*_*man 47 colors bash ksh diff

我想有条件地格式化 Unix 文件,我目前正在处理diff命令,想知道是否可以格式化diff命令输出的文本。

例子:

匹配的值应显示为绿色。
不匹配的值应显示为红色。

假设我有两个文件file1file2我的命令diff file1 file2

现在我希望假设输出包含 5 个不匹配,那么这些不匹配应该以红色显示。如何使用unix实现这一目标?

简而言之,“对于不匹配的值,将 diff 命令的输出颜色更改为红色”

Cir*_*郝海东 57

diff --color 选项已添加到 GNU diffutils 3.4 (2016-08-08)

这是diff大多数发行版的默认实现,很快就会得到它。

Ubuntu 18.04 有diffutils3.6,因此有它。

在 3.5 上,它看起来像这样:

在此处输入图片说明

测试:

diff --color -u \
  <(seq 6 | sed 's/$/ a/') \
  <(seq 8 | grep -Ev '^(2|3)$' | sed 's/$/ a/')
Run Code Online (Sandbox Code Playgroud)

显然在提交 c0fa19fe92da71404f809aafb5f51cfd99b1bee2(2015 年 3 月)中添加。

词级差异

喜欢diff-highlight。似乎不可能,功能请求:https : //lists.gnu.org/archive/html/diffutils-devel/2017-01/msg00001.html

相关主题:

ydiff 这样做,见下文。

ydiff 并排字级差异

https://github.com/ymattw/ydiff

这是涅槃吗?

python3 -m pip install --user ydiff
diff -u a b | ydiff -s
Run Code Online (Sandbox Code Playgroud)

结果:

在此处输入图片说明

如果线条太窄(默认为 80 列),请使用以下内容进行筛选:

diff -u a b | ydiff -w 0 -s
Run Code Online (Sandbox Code Playgroud)

测试文件内容:

一种

1
2
3
4
5 the original line the original line the original line the original line
6
7
8
9
10
11
12
13
14
15 the original line the original line the original line the original line
16
17
18
19
20
Run Code Online (Sandbox Code Playgroud)

1
2
3
4
5 the original line teh original line the original line the original line
6
7
8
9
10
11
12
13
14
15 the original line the original line the original line the origlnal line
16
17
18
19
20
Run Code Online (Sandbox Code Playgroud)

ydiff Git集成

ydiff 无需任何配置即可与 Git 集成。

在 git 存储库中git diff,您可以执行以下操作,而不是:

ydiff -s
Run Code Online (Sandbox Code Playgroud)

而不是git log

ydiff -ls
Run Code Online (Sandbox Code Playgroud)

另见:https : //stackoverflow.com/questions/7669963/how-can-i-get-a-side-by-side-diff-when-i-do-git-diff/14649328#14649328

在 Ubuntu 16.04、git 2.18.0、ydiff 1.1 上测试。

  • [此处](https://www.gnu.org/software/diffutils/manual/diffutils.html#index-color_002c-distinguishing- Different-context)是文档。 (2认同)

Mic*_*mer 24

如果您可以访问 GNU diff,则可以使用其--X-group-format选项来获得该效果,而无需任何其他工具:

diff --old-group-format=$'\e[0;31m%<\e[0m' \
     --new-group-format=$'\e[0;31m%>\e[0m' \
     --unchanged-group-format=$'\e[0;32m%=\e[0m' \
     file1 file2
Run Code Online (Sandbox Code Playgroud)

它使用ANSI 颜色转义码来获得红色和绿色,并在 shell 中引用 ANSI-C来访问\e转义符。

--old-group-format--new-group-format识别非匹配线和用红和色复位码之间插入它们%<%>,而--unchanged-group-format插入件匹配绿色和复位码之间的线。

您也可以使用--old-line-format(etc),代价是每行都有多余的颜色转义:--old-line-format=$'\e[0;31m%L\e[0m'.


小智 11

尝试 colordiff file1 file2

colordiff 在 Linux/BSD 发行版中的可用性

那些运行 Debian 或 Ubuntu(或它们的任何衍生产品)的人可能只需要使用“apt-get install colordiff”来下载和安装;colordiff 还打包用于许多其他 Linux、UNIX 和 BSD 发行版和操作系统。

(引自http://www.colordiff.org/


Tom*_*ale 7

彩色、字级 diff输出

您可以使用以下脚本和diff-highlight执行以下操作:

彩色差异截图

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

(感谢@retracile 的答案突出显示sed


小智 5

如果你安装了 vim,你可以这样做 diff file1 file2 | vim -

Vim 会识别 diff 格式并给它适当的着色。最后的破折号是让 vim 接受来自 diff 命令的输入。

  • 'view'(vim 的只读快捷方式)更适合此目的。 (2认同)