1

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

1
  • Similar question here.
    – Thor
    Sep 8, 2012 at 9:37

6 Answers 6

4

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
3

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

7
  • as the file1 has 50 lines and file2 has 500 lines..this is not working
    – jack
    Sep 7, 2012 at 20:36
  • It should work as per your description i.e. get the list from file2.txt which are identical to file1.txt Sep 7, 2012 at 20:40
  • strange.it doesnt give any output for me..
    – jack
    Sep 7, 2012 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? Sep 7, 2012 at 20:58
  • it outputs empty file
    – jack
    Sep 7, 2012 at 21:01
3

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
1

Assuming that your files are sorted:

comm -12 file1 file2
1
  • 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, 2012 at 10:22
1

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
1

You can also solve this problem using AWK:

NR == FNR {
    line[$1];
    next;
}
$1 in line {
    print $0;
}

As an one liner:

awk 'NR == FNR {line[$1]; next;} $1 in line' file1.txt file2.txt

Be sure to store in memory the smaller file, that is, put it as the first argument of the one liner.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .