Search code examples
linuxshellawkmatchline

How to remove the lines which appear on file 1 from another file 2 KEEPING empty lines?


I know that in other questions you solve the first part of "How to remove the lines which appear on file 1 from another file 2" with: comm -23 file1 file2 and grep -Fvxf file1 file2

But in my case I have empty lines separating data sets that I need to keep, for example:

File 1:

A
B
C

D
E
F

G
H
I

File 2

A

D
F

I

what I want as a result:

B
C

E
    
G
H

The solution can be in bash or csh.

Thanks


Solution

  • With awk please try following once.

    awk 'FNR==NR{arr[$0];next} !NF || !($0 in arr)' file2 file1
    

    Explanation: Adding detailed explanation for above code.

    awk '                  ##Mentioning awk program from here.
    FNR==NR{               ##Checking if FNR==NR which will be TRUE when file2 is being read.
      arr[$0]              ##Creating array with index of $2 here.
      next                 ##next will skip all further statements from here.
    }
    (!NF || !($0 in arr))  ##If line is empty OR not in arr then print it.
    ' file2 file1          ##Mentioning Input_file names here.