22

Possible Duplicate:
how to show lines in common (reverse diff)?

Is there a command to do the opposite of diff? I want to compare two files if the same thing exists in both create a list of them. i am trying to figure out what entry's exist in both files.

Community
  • 1
  • 1
Justin Yoder
  • 527
  • 4
  • 8
  • 17
  • 1
    You might want to look at: http://stackoverflow.com/questions/746458/how-to-show-lines-in-common-reverse-diff and then mark this question as a duplicate. – ArjunShankar Dec 20 '11 at 19:54
  • This might not get closed if you can edit and add specifics, like you want to do this on Linux, etc. – Ken Pespisa Dec 20 '11 at 19:55
  • 1
    I it figured out i was unaware of the comm command before but anyways two lists want to know only what is the same comm -1 -2 file1 file2 > outputfile.txt worked thank you for your help. Probably the easiest answer for what i wanted. – Justin Yoder Dec 20 '11 at 20:15

2 Answers2

36

Here is a solution that WILL NOT change the order of the lines:

fgrep -x -f file1 file2
fge
  • 119,121
  • 33
  • 254
  • 329
  • 3
    This is not a reverse diff, but may be useful nonetheless. It prints the lines in `file2` that exactly match some line in `file1`. `fgrep` searches for fixed string matches `-f` uses file1 as a list of grep search patterns `-x` print only lines matched entirely – Arthur Jul 30 '20 at 15:19
12

Use the join command:

join a.txt b.txt

assuming the files are sorted; if not:

sort a.txt > sorted_a.txt; sort b.txt > sorted_b.txt; join sorted_a.txt sorted_b.txt
Frank Schmitt
  • 30,195
  • 12
  • 73
  • 107