Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do the opposite of diff? [duplicate]

Tags:

linux

shell

diff

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.

like image 956
Justin Yoder Avatar asked Dec 20 '11 19:12

Justin Yoder


People also ask

What is the opposite of diff?

diffinv is the inverse of the diff function.

How do I find the common line of two files?

Use comm -12 file1 file2 to get common lines in both files. You may also needs your file to be sorted to comm to work as expected. Or using grep command you need to add -x option to match the whole line as a matching pattern. The F option is telling grep that match pattern as a string not a regex match.


2 Answers

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

fgrep -x -f file1 file2
like image 137
fge Avatar answered Oct 04 '22 01:10

fge


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
like image 30
Frank Schmitt Avatar answered Oct 04 '22 02:10

Frank Schmitt