Reputation: 41
So I have a file1.txt with a list of names, and a file2.txt with another list of names and I need a list with the names that are in both files.
I tried grep-f file1.txt file2.txt > newlist.txt
but for some reason it isn't working, and the newlist.txt has names that are not in file1.
Does anyone know why this is happening and what i could do to get only the names that are on both lists?
thank you.
Upvotes: 4
Views: 3634
Reputation: 20032
Your grep -f file1.txt file2.txt > newlist.txt
is a nice thought, but will give too much hits when file1.txt has "s10" and file2.txt has "slass100". You want to match the complete line, so try
grep -Fxf file1.txt file2.txt > newlist.txt
This should be faster than a solution that requires sorting first.
Upvotes: 2
Reputation: 96
If file1.txt and file2.txt are sorted, you could use 'comm'
comm -12 file1.txt file2.txt > newlist.txt
Upvotes: 3
Reputation: 23920
If each the names in each list are unique, then you can find their intersection as follows:
sort file1.txt file2.txt | uniq -d > newlist.txt
Upvotes: 1