Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Help direct stdout to grep!

Hi folks,


Hoping someone out there can help a recent Linux transplant.


I am unable to replicate a grep command I use in Linux quite frequently. I would like to take patterns from stdout and pass them to grep to search file foo for those patterns. In linux, I would do the following:


cat patterns | grep -f - foo


If foo contained any patterns, those lines of foo would be happiily printed to stdout. However, this exact command in osx produces the following error:

grep: -: No such file or directory


It looks to me like grep does not recognize the '-' as a sign to take the list of patterns from the piped cat command. Googling for a while now, and can't figure out the problem. In fact, this post suggests there is no problem! Oy.


Please help!

MacBook Air, OS X Mountain Lion (10.8.4)

Posted on Jul 30, 2013 2:34 PM

Reply
Question marked as Best reply

Posted on Jul 30, 2013 2:58 PM

cat patterns | egrep -f foo

egrep -f foo < patterns


OS X uses BSD grep and Linux continues to use GNU grep.

7 replies

Jul 30, 2013 4:04 PM in response to says_anova

@VikingOSX

I think you have it backwards. In your command, you are using foo as a source for patterns and looking for them in patterns via stdin. says_anova wants to read patterns from stdin and look for them in foo.


@says_anova,

Why not just use the basic form "grep -f patterns foo"? If you were doing something more fancy than cat, that would be a problem.

Aug 1, 2013 9:46 AM in response to says_anova

VikingOSX:


The problem is that patterns is stdin, and I am looking for a way to indicate this to grep.


Another solution was provided by a colleague, and it works:


in osx, stdin can be represented by /dev/fd/0, allowing the following workflow:


foo1 has 3 columns. I want to find any instances of patterns from the first column in foo2. So I:


cut -f1 foo1 | grep -f /dev/fd/0 foo2


where /dev/fd/0 represents the output of cut.


Thanks for your help Viking and etresoft!

Help direct stdout to grep!

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.