Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Let's say we have two files.

match.txt: A file containing patterns to match:

fed ghi
tsr qpo

data.txt: A file containing lines of text:

abc fed ghi jkl
mno pqr stu vwx
zyx wvu tsr qpo

Now, I want to issue a grep command that should return the first and third line from data.txt:

abc fed ghi jkl
zyx wvu tsr qpo

... because each of these two lines match one of the patterns in match.txt.

I have tried:

grep -F -f match.txt data.txt

but that returns no results.

grep info: GNU grep 2.6.3 (cygwin)
OS info: Windows 2008 R2

Update: It seems, that grep is confused by the space in the search pattern lines, but with the -F flag, it should be treating each line in match.txt as an individual match pattern.

share|improve this question
    
Which OS are you using, exactly? I see this is tagged Windows, but I've not found the Windows version of grep to be very feature-filled. –  Cakemox Oct 26 '10 at 14:32
    
Question updated with all version info... –  Michael Goldshteyn Oct 26 '10 at 14:36

2 Answers 2

up vote 0 down vote accepted

The solution to this is as follows:

Use the command: tr -d "\r" <match.txt | grep -F -f - text.txt

It seems that grep does not correctly respect windows line endings (CR/LF) for match files presented to it via the -f flag. The tr command can be used to strip carriage returns from the match file and the - special flag can be used with grep, to force it to read the match file from standard input.

share|improve this answer

Since it works on Unix, perhaps you need a later version of grep?

>cat d
abc fed ghi jkl
mno pqr stu vwx
zyx wvu tsr qpo

>cat p
fed ghi
tsr qpo

>grep -F -f p d
abc fed ghi jkl
zyx wvu tsr qpo

what happens if you change the patterns to

fed\040ghi
tsr\040qpo
share|improve this answer
    
I have found a solution, and am posting it as an answer to my own question. It has nothing to do with the version of grep. –  Michael Goldshteyn Oct 26 '10 at 15:22

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.