Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Using "grep -of file1.txt file2.txt" (file contents below), I get output:

and
if
pineapple

Why are 'dif' and 'for' missing? Do I have to use any other switches?

file1.txt

and
dif
for
if
apple
pineapple

file2.txt

andiforpineapple
share|improve this question

migrated from stackoverflow.com May 15 '11 at 6:49

This question came from our site for professional and enthusiast programmers.

add comment

2 Answers

up vote 1 down vote accepted

If you want to re-search the input file for each pattern specified:

$ cat patterns.txt 
and
dif
for
if
apple
pineapple
$ cat source.txt 
andiforpineapple
$ while read; do grep -o -e"$REPLY" source.txt; done <patterns.txt
and
dif
for
if
apple
pineapple

However, this will have a different output line ordering than you apparently want, if the source file has more than one line. Since you've not said what you're using this for, I don't know if that will work for your actual problem.

share|improve this answer
    
Hi Fred, My source.txt file contains a long single string of unknown DNA sequence, and the patterns.txt contains a several small known DNA fragments. –  Naveen May 14 '11 at 5:27
    
I'm very new to shell, so it would be kind of you to break it down. How does this work? while read; do grep -o -e"$REPLY" source.txt; done <patterns.txt –  Naveen May 14 '11 at 5:29
    
@Naveen: I'd use a different tool for that kind of DNA matching. See "help while", "help read", and the "redirection" section in your sh's docs (e.g. man bash) for help on while, read, and <, respectively. –  Fred Nurk May 14 '11 at 5:31
    
@Fred: "I'd use a different tool for that kind of DNA matching." What tools are used for this kind of job? –  Naveen May 15 '11 at 4:49
    
I understood how this works. Thanks! "while read; do grep -oe "$REPLY" source.txt; done < patterns.txt" –  Naveen May 15 '11 at 4:50
show 2 more comments

Its like this:

andiforpineapple
  ^found and
   ^continuing search from i
    ^found if
     ^continuing search from o
       ^found pineapple
share|improve this answer
    
Hi Richard, Is there a way where I can force the search to start from the beginning of the string? –  Naveen May 14 '11 at 4:47
    
There are many implementations of grep. Which are you using? –  Richard Brightwell May 14 '11 at 4:48
    
I'm using this version for grep, GNU grep 2.6.3, and I'm running this os, Ubuntu 11.04 –  Naveen May 14 '11 at 5:07
    
I don't see a command line option for this and unfortunately my Linux VM is dead at the moment. I would like to experiment with enhancing the regular expressions to use the \A anchor to match the start of the regex. You might try that. Here is a good reference site on the web. –  Richard Brightwell May 14 '11 at 12:13
    
@Richard: Thanks for the idea. –  Naveen May 15 '11 at 4:48
add comment

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.