Search code examples
shellsortingunixsetutility

unix utility to compare lists and perform a set operation


I believe what I'm asking for is a sort of set operation. I need help trying to create a list of the following:

List1 contains:

1
2
3
A
B
C

List2 contains:

1
2
3
4
5
A
B
C
D
E

(I need this) - The Final list I need would be (4) items:

4
5
D
E

So obviously List2 contains more elements than List1. Final list which I needs are the elements in List2 that are NOT in List1.

Which linux utility can I use to accomplish this? I have looked at sort, comm but i'm unsure how to do this correctly. Thanks for the help


Solution

  • Using awk with a straight forward logic.

    awk 'FNR==NR{a[$0]; next}!($0 in a)' file1 file2
    4
    5
    D
    E
    

    Using GNU comm utility, where according to the man comm page,

    comm -3 file1 file2
          Print lines in file1 not in file2, and vice versa.
    

    Using it for your example

    comm -3 file2 file1
    4
    5
    D
    E