两种文件之间输出差异的方法(最好使用命令行)

the*_*_mr 27 comparison diff

我熟悉tkDiff和WinMerge等工具,并且知道如何查看两个文件之间的区别.

我要做的是在一个文件中生成一个不存在于另一个文件中的元素的报告.

例如:

File1包含:

apple
cool
dude
flan
Run Code Online (Sandbox Code Playgroud)

File2包含:

apple
ball
cool
dude
elephant
Run Code Online (Sandbox Code Playgroud)

我想生成一份包含以下内容的报告:

ball
elephant
Run Code Online (Sandbox Code Playgroud)

或者,更好的是,这样的报告:

+ball
+elephant
-flan
Run Code Online (Sandbox Code Playgroud)

有人知道可以做到这一点的工具吗?最好使用命令行选项.

WinMerge中的报告功能并不是我想要的,但没有命令行选项来做到这一点(据我所知).

提前致谢.

pot*_*ong 45

这可能适合你(GNU diff):

diff -u file1 file2 | sed -n '1,2d;/^[-+]/p'
+ball
-flan
+elephant
Run Code Online (Sandbox Code Playgroud)


The*_*aul 26

你可能想要Unix comm实用程序.Windows版本包含在gnuwin32中

名称

   comm - compare two sorted files line by line
Run Code Online (Sandbox Code Playgroud)

概要

   comm [OPTION]... FILE1 FILE2
Run Code Online (Sandbox Code Playgroud)

描述

   Compare sorted files FILE1 and FILE2 line by line.

   With  no    options,  produce  three-column  output.  Column one contains
   lines unique to FILE1, column two contains lines unique to  FILE2,  and
   column three contains lines common to both files.

   -1     suppress lines unique to FILE1

   -2     suppress lines unique to FILE2

   -3     suppress lines that appear in both files
Run Code Online (Sandbox Code Playgroud)

  • 重定向它,与其他任何CLI命令相同。例如,“ comm -1 file1 file2> results” (2认同)