I have a file with a list of keywords I would like to locate (pattern.txt):
foo
foo_bar
asdf
asdf_fdsa
Some of these keywords are substrings of others, so I am using the -w option for grep to match full words.
For this example, I am just using a copy of the pattern file for the data to search (data.txt).
When I run grep -wf pattern.txt data.txt
I would expect all patterns to be found, but the result is only the two smaller patterns:
foo
asdf
However, if I re-order the pattern file to list the long-words before short-words:
foo_bar
foo
asdf_fdsa
asdf
grep -wf pattern.txt data.txt
will return all four matches. What gives? Why does the ordering of the pattern file change the output here?
After research, I can tell that -f is shorthand for writing grep -e ... -e ... etc.
, and can confirm that this behavior is reflected when written in this form, but I cannot find any info about this order-dependent behavior. Thanks for any insight.
Edit: on macOS with BSD grep 2.5.1-FreeBSD