4

Im trying to find which lines differ in between file1.csv and file2.csv. if file1.csv looks like this:

1,2,3,4
5,6,7,8
9,10,11,12

and file2.csv looks like this:

1,2,3,4
9,9,9,9
5,6,7,8
9,9,11,12

What is a command I can use to pull those unique lines out? Also would diff, comm, or grep be the best tool?

0

1 Answer 1

3

You are probably looking for comm:

$ comm <(sort file1.csv) <(sort file2.csv)
        1,2,3,4
        5,6,7,8
9,10,11,12
    9,9,11,12
    9,9,9,9

By default, it will print lines present only in the first file as the 1st column, those present in both as the second column and those only in the 2nd file as the third column. You can use -1, -2 or -3 to suppress any of those columns. For example:

  • Show only those that are in both files:

    $ comm -12 <(sort file1.csv) <(sort file2.csv) 
    1,2,3,4
    5,6,7,8
    
  • Show only lines present in the first file, but not in the second:

    $ comm -32 <(sort file1.csv) <(sort file2.csv) 
    9,10,11,12
    
  • Show only lines present in the second file, but not in the first:

    $ comm -31 <(sort file1.csv) <(sort file2.csv) 
    9,9,11,12
    9,9,9,9
    

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .