Read expression for grep from standard input

GrepStdin

Grep Problem Overview


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/"

Grep Solutions


Solution 1 - Grep

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

$ echo Content | grep -f - notice.html 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
...

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDorView Question on Stackoverflow
Solution 1 - GrepthkalaView Answer on Stackoverflow