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.

I want to find patterns that are listed in one file and find them in other file. The second file has those patterns separated by commas.

for e.g. first file F1 has genes

ENSG00000187546
ENSG00000113492  
ENSG00000166971

and second file F2 has those genes along with some more columns(five columns) which I need

 region     gene           chromosome  start       end

 intronic   ENSG00000135870 1   173921301   173921301
intergenic  ENSG00000166971(dist=56181),ENSG00000103494(dist=37091) 16 53594504    53594504
ncRNA_intronic  ENSG00000215231 5   5039185 5039185
intronic    ENSG00000157890 15  66353740    66353740

So the gene ENSG00000166971 which is present in the second file does not show up in grep because it has another gene with it,separated by comma.

My code is:

grep -f "F1.txt" "F2.txt" >output.txt

I want those values even if one of them is present,and the associated data with it.Is there any way to do this?

share|improve this question
1  
Is it possible that your implementation of grep anchors its patterns by default? Does grep -f <(echo a) <(echo 'a,b') produce any output? –  Joseph R. Feb 14 at 21:34

1 Answer 1

up vote 2 down vote accepted

What version of grep are you using? I tried your code and got the following results:

$ grep -f file1 file2
ENSG00000187546
ENSG00000113492
ENSG00000166971,ENSG00000186106

If you just want the results that match you can use grep's -o switch to report only the things that match:

$ grep -o -f file1 file2 
ENSG00000187546
ENSG00000113492
ENSG00000166971

grep version

$ grep --version
grep (GNU grep) 2.14
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Mike Haertel and others, see <http://git.sv.gnu.org/cgit/grep.git/tree/AUTHORS>.

Stray characters in F1.txt?

While debugging this further I noticed several stray spaces at the end of the 2nd line in the file F1.txt. You can see them using hexdump.

$ hexdump -C ff1
00000000  45 4e 53 47 30 30 30 30  30 31 38 37 35 34 36 0a  |ENSG00000187546.|
00000010  45 4e 53 47 30 30 30 30  30 31 31 33 34 39 32 20  |ENSG00000113492 |
00000020  20 0a 45 4e 53 47 30 30  30 30 30 31 36 36 39 37  | .ENSG0000016697|
00000030  31 0a                                             |1.|
00000032

They show up with as ASCII codes 20. You can see them in them here: 32 20 20 0a.

share|improve this answer
    
grep --version GNU grep 2.6.3 Copyright (C) 2009 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <gnu.org/licenses/gpl.html>; This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. I am only getting the matches if they exist as individuals and not as you said.Strangely,The only match I am getting is the last pattern from the file containing the patterns to be matched. –  Ron Feb 14 at 21:29
    
I have pasted more complete version of my second file although with less rows,and tried searching for it.It does not match the second row from second file –  Ron Feb 14 at 21:39
    
@Ron - You have stray characters at the end of the strings in the F1.txt file that's causing your issue. Just confirmed it works fine on grep (GNU grep) 2.5.1 –  slm Feb 14 at 21:50
    
okay.I saw those characters now.how should I remove these characters then? using tr? –  Ron Feb 14 at 22:00
    
I just saved the file in text wrangler in Unix-LF format,and now it works!! –  Ron Feb 14 at 22:17

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.