[Shell] compare the contents of two files

comm -3 file1 file2

Function: Progressive compare two sorted files, there are three default output: only some lines in file1, file2 only some lines in, the file1 and file2 common to the line. There the -1, -2, -3, respectively, to suppress an output a corresponding column. For example, practical -3 parameters, and file1 file2 are not common to the output portion. That we can achieve the purpose of this article.
But note that two files to, comm comparison sorted, comm when dealing with documents, first check to see whether the file is ordered, for example, file1 and file2 reads as follows:

 

  1. $cat file1  
  2. line1  
  3. line2  
  4. line3   

  1. $cat file2  
  2. Line0  
  3. line1  
  4. line3  
  5. line2  

When you call comm command, you are prompted disorder when file2 file, the resulting output is as follows:

$ comm -3 file1 file2
        line0
line2
comm: file 2 is not in sorted order
        line2

If --nocheck-order parameters, ordering detection is not performed, the following results:

$ comm -3 --nocheck -order file1 file2

        line0
        line2

        line2


Statistical same row two text files

          grip -Ff file1 file2

Statistics file2 there, file1 not in line

         grip -vFf file2 file1

How to compare two files and remove the same content

        for i in $(<file1); do grep $i file2 || echo $i >>tmp1 ; done

Output same line:

        grip -wf file1 file2

Output different lines:

        grip -wvf file1 file2

 

ref : link1

Published 89 original articles · won praise 17 · views 40000 +

Guess you like

Origin blog.csdn.net/lbt_dvshare/article/details/89632392