Linux小技巧之文件差异对比
  zwmX4WA4Dykt 2023年11月19日 53 0


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


目录

  • diff
  • comm
  • 只查看file1 中差异文件
  • 只查看file2中差异文件
  • 查看两个文件相同部分


先构造两个文件:

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


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月19日 0

暂无评论

推荐阅读
zwmX4WA4Dykt