提起文件差异对比,最常想到的是diff,如果内容少还好,内容多的话就不太友好,其实 Linux 下还有一个小工具能够更加方便的对比两个文件。
下面分别进行介绍:

先构造两个文件:

for i in {a..z} ; do echo $i; done >file1
for i in {h..z} ; do echo $i; done >file2
for i in {1..5} ; do echo $i; done >>file2

diff

先看下命令的说明:

diff - compare files line by line

# diff file1 file2
1,7d0
< a
< b
< c
< d
< e
< f
< g
26a20,24
> 1
> 2
> 3
> 4
> 5

如果只想查找存在 file1 中的差异内容,需要进行过滤

# diff file1 file2|grep '^<'
< a
< b
< c
< d
< e
< f
< g

comm

comm - compare two sorted files line by line

使用 comm 比较前,最好对文件内容进行排序。

comm 相关参数:

   -1     suppress column 1 (lines unique to FILE1)
   -2     suppress column 2 (lines unique to FILE2)
   -3     suppress column 3 (lines that appear in both files)

直接看例子:

只查看file1 中差异文件

# comm -23 file1 file2
a
b
c
d
e
f
g
comm: file 2 is not in sorted order
comm: input is not in sorted order

只查看file2中差异文件

# comm -13 file1 file2
comm: file 2 is not in sorted order
1
2
3
4
5
comm: input is not in sorted order

查看两个文件相同部分

# comm -12 file1 file2
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
comm: file 2 is not in sorted order
comm: input is not in sorted order
GitHub 加速计划 / li / linux-dash
6
1
下载
A beautiful web dashboard for Linux
最近提交(Master分支:4 个月前 )
186a802e added ecosystem file for PM2 4 年前
5def40a3 Add host customization support for the NodeJS version 4 年前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐