Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

file1.txt (50 lines)

TERYUFV00000010753
TERYUFV00000009526 

file2.txt (500 lines)

TERYUFV00000009526 refids_739_known_8/10_target
TERYUFV00000018907 refids_12023_known_21/22_target
TERYUFV00000010753 refids_11775_known_1/1_target

Output.txt

TERYUFV00000010753 refids_11775_known_1/1_target
TERYUFV00000009526 refids_739_known_8/10_target

Compare file1.txt (has 50 lines) with file2.txt (has 500 lines), get the list from file2.txt which are identical to file1.txt.

I tried both join & fgrep command and it outputs empty file

share|improve this question
    
Similar question here. –  Thor Sep 8 '12 at 9:37

5 Answers 5

up vote 3 down vote accepted

fgrep -f file1.txt file2.txt

Here we are obtaining search pattern from file1.txt and searching it in file2.txt. As the text is fixed we are using fgrep for faster search operation.

enter image description here

share|improve this answer
    
as the file1 has 50 lines and file2 has 500 lines..this is not working –  jack Sep 7 '12 at 20:36
    
It should work as per your description i.e. get the list from file2.txt which are identical to file1.txt –  Sachin Divekar Sep 7 '12 at 20:40
    
strange.it doesnt give any output for me.. –  jack Sep 7 '12 at 20:49
    
Don't know why it is not working for you. I just uploaded a screenshot of my terminal where you can see the required output. Is it giving any error when you execute the command? –  Sachin Divekar Sep 7 '12 at 20:58
    
it outputs empty file –  jack Sep 7 '12 at 21:01

Assuming that your files are sorted:

comm -12 file1 file2
share|improve this answer
    
Did you test your method before posting it here? It doesn't work for this question. comm compares the two sorted files by the entire line. So, a line of "TERYUFV00000010753" in file1 is different from a line of "TERYUFV00000010753 refids_11775_known_1/1_target" in file2. –  Dejian Sep 12 '12 at 10:22

Does the following line work?

grep -iw -f file1.txt file2.txt

If the files were uploaded to the server from a Windows client, maybe you should run the dos2unix first.

dos2unix file1.txt file2.txt

If the above commands do not work, you can try the following lines to see whether there are extra nonprinting characters at the beginning or end of the lines in file1.txt. The extra nonprinting characters in items of file1.txt may lead to the failure of grep from file2.txt.

cat -v file1.txt
sed -n -l file1.txt
share|improve this answer

When you use join the entries on each line are like "cells" in a db, but they should be sorted, so you can try,

sort file1.txt > file1_t.txt
sort file2.txt > file2_t.txt

And then do the join

$ join file1_t.txt file2_t.txt

which will give you an external join, i.e. a list of all the occurrences of the cells in both files. To reduce this list to only the entries in both files, pipe the output of the above command into uniq

$ join file1_t.txt file2_t.txt | uniq
share|improve this answer

You need to sort before you join.

$ cat a.in
TERYUFV00000010753
TERYUFV00000009526
$ cat b.in
TERYUFV00000009526 refids_739_known_8/10_target
TERYUFV00000018907 refids_12023_known_21/22_target
TERYUFV00000010753 refids_11775_known_1/1_target
$ join a.in b.in
$ join <(sort a.in) <(sort b.in)
TERYUFV00000009526 refids_739_known_8/10_target
TERYUFV00000010753 refids_11775_known_1/1_target
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.