I have two files - one with some IDs (text file) and other with IDs and descriptions (tab file).




chr10 31120 A T Descriptions
Chr11 131120 A T Descriptions
chr12 23765090 G C Descriptions
chr15 784930 A G Descriptions 

Expected Output:

chr10 31120 A T Descriptions

Present output:

Random selected columns some matching the file1 IDs and some not.

I am using grep -f file1 file2 > output.txt to match my IDs from file1 and extract their info from file2. However the problem lies, when I have an ID say- 31120. It find me IDs like 31120, 231120, 311200 etc.

Since I am running one file to find patterns in another what can I use to make it specific to my search so that only 31120 is returned. While it is easy to do so in a regular one liner grep search, I don't know how I can do that when 2 files are being compared.

I have tried using grep -F -f file1 file2 but that did not work.

Now I used grep -Fwf file1 file2, which is not working either.

  • can you please post your expected output ? – Rahul Aug 5 '16 at 11:51
  • Are these IDs always in the 2nd column in file2 ? – don_crissti Aug 5 '16 at 11:53
  • 2
    use -w option to match only against words... – Sundeep Aug 5 '16 at 12:03
  • 1
    @ilkkachu - or this one – don_crissti Aug 5 '16 at 12:12
  • 5
    but yeah, grep -Fwf file1 file2 should work, IF the descriptions don't contain matches for the id's. – ilkkachu Aug 5 '16 at 12:13

You need to add -w to match whole words only. As you don't have patterns but fixed string, you may add -F.

$ grep -wFf file1 file2
chr10 31120 A T Descriptions

Another good tool for this job is join, but by default it will print the join field first and then the remaining fields, which may not be what you like:

$ join -2 2 file1 file2
31120 chr10 A T Descriptions

-2 2 means join field for second file is 2.

Beware that for join, the inputs need to be sorted lexically on the join field. If they're not sorted already and if your shell supports process substitution (AT&T ksh, zsh, bash), you can do:

join -2 -2 <(sort -k1,1 file1) <(sort -k2,2 file2)
  • How to upvote an edit ? Thanks @Stéphane. Somehow I always forget about the join/sort thing ... – pLumo Aug 9 '19 at 9:00

As others suggested grep -Fwf file1 file2 should work.

If you want to use loops, try

for i in `cat file1`; do  grep " "${i}" " file2; done
  • 1
    This 1) uses $i unquoted and 2) calls grep once for each word in file1 which will be extremely slow (as it needs to read the entire file2 every time). – Kusalananda Aug 9 '19 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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