0

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

File1:

31120
211890
542312

File2:

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
  • 1
    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
0

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

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.