-1

I have a couple of files with names and need to find names that occur in all of them. E.g.

   File 1    |   File 2   | File 3
   --------------------------------
   A         |  B         |  C
   S         |  A         |  T
   T         |  O         |  W
   G         |  F         |  I
   R         |  X         |  A

In this case it should give me 'A' since all files contain it. The order doesn't matter, but it should find all names that occur in all files and not stop after it found the first!

Is there a way to do this with the command line?

wasp256
  • 5,943
  • 12
  • 72
  • 119

2 Answers2

1

This should do:

grep -Fxf file1 file2 | grep -Fxf file3

Omitting the -F or -x options may give unexpected results with empty lines and substring matches as outlined in comments to this answer.

Community
  • 1
  • 1
Norman
  • 1,975
  • 1
  • 20
  • 25
0

Since the files are unsorted, the next command is slower than grep.
When you need to sort the file or want to see how you can avoid temp files, you might like

comm -12 <(comm -12 <(sort file1) <(sort file2) ) <(sort file3)
Walter A
  • 19,067
  • 2
  • 23
  • 43