grep -f generating unexpected output

2

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

Naveen

Posted 2011-05-14T04:29:53.803

Reputation: 23

Answers

1

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.

Fred Nurk

Posted 2011-05-14T04:29:53.803

Reputation: 126

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. – None – 2011-05-14T05:27:37.820

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 – None – 2011-05-14T05:29:27.350

@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 – 2011-05-14T05:31:46.213

@Fred: "I'd use a different tool for that kind of DNA matching." What tools are used for this kind of job? – None – 2011-05-15T04:49:39.663

I understood how this works. Thanks! "while read; do grep -oe "$REPLY" source.txt; done < patterns.txt" – None – 2011-05-15T04:50:49.200

@Naveen: I don't do DNA matching, so I'm not really sure what other tools exist... :) But I have seen other questions about it, and this definitely feels like the wrong choice. – Fred Nurk – 2011-05-15T04:57:53.680

@Fred: Thanks for the info. I'll search for DNA matching tools on the net. – Naveen – 2011-05-18T05:22:03.133

3

Its like this:

andiforpineapple
  ^found and
   ^continuing search from i
    ^found if
     ^continuing search from o
       ^found pineapple

Richard Brightwell

Posted 2011-05-14T04:29:53.803

Reputation: 149

Hi Richard, Is there a way where I can force the search to start from the beginning of the string? – None – 2011-05-14T04:47:58.070

There are many implementations of grep. Which are you using? – Richard Brightwell – 2011-05-14T04:48:37.570

I'm using this version for grep, GNU grep 2.6.3, and I'm running this os, Ubuntu 11.04 – None – 2011-05-14T05:07:07.883

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 – 2011-05-14T12:13:16.907