0

I am using FreeBSD grep, version 2.5.1, and want to be able to have grep read the pattern from stdin. Other versions of grep I have used allow for the specification of - to enable this feature. However, the version I have does not recognize this option. Is there an alternative option I simply missed in the "man page" or does FreeBSD grep, 2.5.1, not support such capability?

  • You're talking about grep -f -? – Mikel May 23 '13 at 2:54
  • Do you mean pattern from stdin, as in printf pattern | grep -f - files_to_search, or haystack from stdin as in: tail file_to_search | grep pattern - ? – Runium May 23 '13 at 4:21
3

I suspect by "other versions of grep" you mean GNU grep and are thinking of its -f option, which allows a - argument to make it behave as you say.

BSD grep does support -f, but it requires an actual file as an argument. It indeed won't take - as an argument.

All is not lost. You can still pass the pattern to grep via command substitution:

$ grep `my-pattern-producing-command` some-files*

Or if the pattern is in a file:

$ grep `head -1 my-pattern-file.txt` some-files*

Or, just install GNU grep:

$ cd /usr/ports/textproc/gnugrep
$ sudo make install clean

By the way, SysV grep doesn't support -f at all, so that's another reason to be careful about relying on it. That said, Solaris is now shipping GNU grep by default. You actually have to go out of your way to use SysV grep on Solaris.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.