0

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

noober
  • 1,427
  • 3
  • 23
  • 36
  • Did you make an attempt, there are plenty of questions around for similar logic. – Inian Dec 14 '16 at 06:54
  • Possible duplicate of [find difference between two text files with one item per line](http://stackoverflow.com/questions/4078933/find-difference-between-two-text-files-with-one-item-per-line) – Inian Dec 14 '16 at 07:11

3 Answers3

1

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
Inian
  • 80,270
  • 14
  • 142
  • 161
0

You can do it with a simple grep command inverting the match with -v and reading the search terms from list1 with -f, e.g. grep -v -f list1 list2. Example use:

$ grep -v -f list1 list2
4
5
D
E

Linux provides a number of different ways to skin this cat.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0

You can try this :

$ diff list1.txt list2.txt | egrep '>|<' | awk '{ print $2 }' | sort -u
4
5
D
E

i hope help you

Ahmed
  • 325
  • 1
  • 8