What is the most efficient way to get the intersection between two files , there is :
sort file1 file2 | uniq -d
What is the most efficient way to get the intersection between two files , there is :
sort file1 file2 | uniq -d
Use
grep
command line as following:If you want to combine the contents of two files then run the below command,
Let file1 = 'A\nA' and file2 = 'A\nA\nA'. Is the intersection just 'A' or 'A\nA'? In other words, is the number of times the same line is appears in both the files relevant?
If it is just 'A' this will work:
The problem with the approach given in the question is that even if a line doesn't exist in both files, if it is repeated in one it will appear in the result. So, we need to remove repetitions within files first using
sort fileX | uniq
.If it is 'A\nA' then this will work:
I can think of a few more one liners (with awk, sort, uniq and cut) but none of them is much simpler than this. This, of course, doesn't mean there aren't any simpler solutions.