Sign up ×

How can I make grep read the expression from standard input (stdin)?

For example (the following doesn't work):

grep -i -f &0 /path/to/text/file < "/regexp/"
share|improve this question

1 Answer 1

up vote 17 down vote accepted

Use -f with a single dash to denote the standard input:

$ echo Content | grep -f - notice.html 

Note: This has been tested with GNU grep - I am not sure if it's specified by POSIX.

share|improve this answer
@Dor: what about using -E for extended expressions? Doesn't that work? –  thkala Jul 10 '11 at 21:22
doesn't work. neither egrep –  Dor Jul 11 '11 at 7:52
This works fine on MinGW (Windows) - as long as the free-standing dash is included! –  Peter Mortensen Nov 21 '14 at 9:33
Actually, I get the expected matches but also false positives from grep that I can not explain. The search specification (from standard input) is a series of numbers (like 1.174500, 45045.367300, 45045.442300, 45045.502500, 45675.290200, 48149.824300, 48149.883600 and 52485.123600) and I use the "-A" and "-B" options for grep: "grep -f - -A 10 -B 10 <file>". I get false positive matches from places far apart in <file>. –  Peter Mortensen Nov 21 '14 at 12:57
This probably has been solved a long time ago. If you search for 45045.502500, you do realize the . is a character wildcard, don't you? –  jcoppens Jul 6 at 0:21

Your Answer


