在我们之前的一篇文章中,我们已经解释过对于Linux系统。我们列出了命令行和 GUI 工具的组合,用于比较和查找文件之间的差异,每个工具都具有某些显着的功能。 Linux 的另一个有用的 diff 实用程序称为聊天。
另请阅读:
聊天是一个简单的命令行实用程序,用于显示两个文件之间的差异并交互合并。它易于使用,并具有简单的使用选项,如下所述。
使用的语法聊天如下。
$ sdiff option... file1 file2
在 Linux 中显示两个文件之间的差异
1.最简单的跑步方式聊天是提供您要比较的两个文件名。它将并排显示合并后的差异,如以下屏幕截图所示。
$ cal >cal.txt $ df -h >du.txt $ sdiff du.txt cal.txt

将所有文件视为文本文件
2.要将所有文件视为文本并逐行比较它们,无论它们是否是文本文件,请使用-a旗帜。
$ sdiff -a du.txt cal.txt Filesystem Size Used Avail Use% Mounted on | April 2018 udev 3.9G 0 3.9G 0% /dev | Su Mo Tu We Th Fr Sa tmpfs 788M 9.7M 779M 2% /run | 1 2 3 4 5 6 7 /dev/sda10 324G 265G 43G 87% / | 8 9 10 11 12 13 14 tmpfs 3.9G 274M 3.6G 7% /dev/shm | 15 16 17 18 19 20 21 tmpfs 5.0M 4.0K 5.0M 1% /run/lock | 22 23 24 25 26 27 28 tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup | 29 30 /dev/loop2 82M 82M 0 100% /snap/core/4206 | /dev/loop4 181M 181M 0 100% /snap/vlc/190 < /dev/loop1 87M 87M 0 100% /snap/core/4407 < /dev/loop0 189M 189M 0 100% /snap/vlc/158 < /dev/loop3 83M 83M 0 100% /snap/core/4327 < cgmfs 100K 0 100K 0% /run/cgmanager/fs < tmpfs 788M 40K 788M 1% /run/user/1000 <
忽略制表符和空白
3.如果您的文件有太多空格,您可以看出聊天使用比较时忽略所有空白-W转变。
$ sdiff -W du.txt cal.txt
4.你还可以告诉聊天使用以下命令忽略行尾的任何空白-z选项。
$ sdiff -z du.txt cal.txt
5.此外,您还可以指导聊天忽略由于选项卡扩展而引起的更改-E旗帜。
$ sdiff -E du.txt cal.txt
比较差异时忽略大小写
6.忽略大小写(其中聊天将大写和小写视为相同),使用-i选项如图所示。
$ sdiff -i du.txt cal.txt
比较差异时忽略空行
7.这-B选项有助于忽略文件中的空白行。
$ sdiff -B du.txt cal.txt
定义要输出的列数
8. 聊天允许您设置要打印的列数(默认为130),通过使用-w切换如下。
$ sdiff -w 150 du.txt cal.txt
将制表符展开为空格
9.要将输出中的制表符扩展到空格,请使用-t选项。
$ sdiff -t du.txt cal.txt
交互式运行 sdiff
10.这-o标志使其能够以交互方式运行并将输出发送到文件。在此命令中,输出将发送到sdiff.txt文件,按进入看到后%登录,获取交互式菜单。
$ sdiff du.txt cal.txt -o sdiff.txt
Filesystem Size Used Avail Use% Mounted on | April 2018
udev 3.9G 0 3.9G 0% /dev | Su Mo Tu We Th Fr Sa
tmpfs 788M 9.7M 779M 2% /run | 1 2 3 4 5 6 7
/dev/sda10 324G 265G 43G 87% / | 8 9 10 11 12 13 14
tmpfs 3.9G 274M 3.6G 7% /dev/shm | 15 16 17 18 19 20 21
tmpfs 5.0M 4.0K 5.0M 1% /run/lock | 22 23 24 25 26 27 28
tmpfs 3.9G 0 3.9G 0% /sys/fs/cgroup | 29 30
/dev/loop2 82M 82M 0 100% /snap/core/4206 |
/dev/loop4 181M 181M 0 100% /snap/vlc/190 <
/dev/loop1 87M 87M 0 100% /snap/core/4407 <
/dev/loop0 189M 189M 0 100% /snap/vlc/158 <
/dev/loop3 83M 83M 0 100% /snap/core/4327 <
cgmfs 100K 0 100K 0% /run/cgmanager/fs <
tmpfs 788M 40K 788M 1% /run/user/1000 <
%
ed: Edit then use both versions, each decorated with a header.
eb: Edit then use both versions.
el or e1: Edit then use the left version.
er or e2: Edit then use the right version.
e: Discard both versions then edit a new one.
l or 1: Use the left version.
r or 2: Use the right version.
s: Silently include common lines.
v: Verbosely include common lines.
q: Quit.
%
请注意,您需要一些编辑器,例如编辑在这种情况下,在使用它们之前先将其安装在您的系统上。
调用另一个程序来比较文件
11.这--diff-programswitch 允许您调用另一个命令行工具,除了聊天本身来比较文件,例如,您可以调用差异程序如图所示。
$ sdiff --diff-program=diff du.txt cal.txt
有关更多信息,请参阅 sdiff 手册页。
$ man sdiff
在这篇文章中,我们研究了聊天适合初学者的命令行工具示例。如果您有任何疑问,请使用下面的评论表联系我们。
