逐行比较两个文件并在另一个文件中生成差异 [英] Compare two files line by line and generate the difference in another file

查看:49
本文介绍了逐行比较两个文件并在另一个文件中生成差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较文件 1 和文件 2 并生成一个文件 3,其中包含文件 1 中文件 2 中不存在的行.

I want to compare file1 with file2 and generate a file3 which contains the lines in file1 which are not present in file2.

推荐答案

diff(1) 不是答案,但 comm(1) 是.

diff(1) is not the answer, but comm(1) is.

NAME
       comm - compare two sorted files line by line

SYNOPSIS
       comm [OPTION]... FILE1 FILE2

...

       -1     suppress lines unique to FILE1

       -2     suppress lines unique to FILE2

       -3     suppress lines that appear in both files

所以

comm -2 -3 file1 file2 > file3

输入文件必须排序.如果不是,请先对它们进行排序.这可以通过临时文件来完成,或者...

The input files must be sorted. If they are not, sort them first. This can be done with a temporary file, or...

comm -2 -3 <(sort file1) <(sort file2) > file3

前提是你的 shell 支持进程替换(bash 支持).

provided that your shell supports process substitution (bash does).

这篇关于逐行比较两个文件并在另一个文件中生成差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆