Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read expression for grep from standard input

Tags:

grep

stdin

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/" 
like image 467
Dor Avatar asked Jul 10 '11 14:07

Dor


People also ask

Does grep read from STDIN?

While in the later case when you just do grep 'something' , there no file name given and hence grep will read its STDIN for input, no pipe is involved here so you need to type something on the STDIN (give input) to see the output. As you can see grep matched 46 from my typed contents and showed it on STDOUT.

How do I use grep to read a file?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.

How do you grep the output of a command in shell script?

The -n ( or --line-number ) option tells grep to show the line number of the lines containing a string that matches a pattern. When this option is used, grep prints the matches to standard output prefixed with the line number.


1 Answers

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.

like image 116
thkala Avatar answered Oct 12 '22 10:10

thkala