Linux中的对比工具,Linux文件对比工具—comm/egrep/diff

本文介绍了Linux中用于文件对比的三个命令行工具:comm、egrep和diff。通过示例展示了如何使用它们来找出文件间的差异、共同内容以及各自独有的部分。例如,comm命令配合参数可以显示两个文件的交集和并集,egrep则能实现类似的功能。同时,diff命令详细对比了文件的相同和不同之处,提供了查看具体差异的选项。这些工具对于日常的文件管理和文本分析非常实用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、comm

带参数:

-1 不显示只在第1个文件里出现过的行。

-2 不显示只在第2个文件里出现过的行。

-3 不显示同时在第1和第2个文件里出现过的行。

comm对比的两个文件,要求先通过sort排序。不加参数时分“三列”显示,第一列是只存在于“第一个”文件中的内容,第二列是只存在于“第二个”文件中的内容,第三列是两个文件相同部分。

root@master # cat file1

a

b

c

root@master # cat file2

a

c

d

root@master # comm -12 file1 file2

a

c

2、egrep

egrep -f file1 file2 相当于 comm -12 file1 file2文件相同部分

root@master # egrep -f file1 file2

a

c

egrep -f file2 -v file1 相当于 comm -23 file1 file2 得到只存在于第一个文件中的部分

root@master # egrep -f file2 -v file1

b

egrep -f file1 -v file2 相当于 comm -13 file1 file2 得到只存在于第二个文件中的部分

root@master # egrep -f file1 -v file2

d

3、diff

diff可以比较两个文件,或者文件夹,着重于两个文件的相同部分和不同部分的统计。

diff -y file1 file2 | egrep -v '||' | awk '{print $1}'得到两个文件相同部分

root@master # diff -y file1 file2|egrep -v '|\|'|awk '{print $1}'

a

c

diff file1 file2 |grep '

root@master # diff file1 file2 |grep '

b

diff file1 file2 |grep '>'|awk '{print $2}'得到只存于第二个文件

root@master # diff file1 file2 |grep '>'|awk '{print $2}'

d

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值