grep stands for Global Regular Expression Print. It is used to filter a large input in order to get the required pattern. In other words, we can find some patterns using the this command.

The options for grep:


     Besides all these options, we also usually use a pipe to let the grep takes its input from the standard output. For example,
% who | grep isc50183
% ls -l | grep '^d........x'
To redirect the results of a search to a file:
% grep smith /etc/passwd > smurffs
To search a file for a simple text string:
% grep copying help
This searches the file 'help' for the string 'copying' and displays each line on your terminal.

To search a file using regular expressions:
% grep regular_expression filename
     When passing the regular expression into grep, the shell will expand metacharacters before grep. For example, if we want to grep all the ocurrances of dollor sign ($) in a file, we cannot just type:
% grep $ filename
     The shell will expand the special meaning of '$' which means variable. Since the variable is null, we are actually grepping null chracter from the file now. We cannot use these commands either:
% grep '$' filename
% grep \$ filename
The correct way of doing this is actually:
% grep '\$' filename
Another example of a regular expression on the command line:
% grep -n '[dD]on\'t' tasks
     This uses a regular expression to find and display each line in the file 'tasks' that contains the pattern 'don't' or 'Don't'. The line number for each line is also displayed. The expression is quoted to prevent the shell from expanding the metacharacters '[', ']' and '''. Double quotes are used to quote the single quote in dDon't.

grep: General Regular Expression Parser

Syntax:

grep [options]  [one or more file(s)]

If several files are mentioned, their name will precede each matching
line displayed in the result. Use the -h option to not display these
names; use the -l option to get nothing but the matching filenames. It
can be useful, especially in long argument lists, to browse the files
with a shell loop and use the grep {pattern} [filename] /dev/null trick.

The pattern is a regular expression, even though most of the time it
consists of a simple word.  The most frequently used options are the
following:

  -i - Make a case insensitive search.
  -v - Invert search: display lines which do not match the pattern.
  -n - Display the line number for each line found.
  -w - Tells grep that the pattern should match a whole word.

In case you want to use grep in a pipe, you don't have to specify the
filename as, by default, it takes its input from the standard input.
Similarly, by default, it prints the results on the standard output, so
you can pipe the output of a grep to yet another program without fear.

Example:

  $ cat /usr/doc/HOWTO/Parallel-Processing-HOWTO | \
  grep -n thread | less