Documented in Volume 1 of the UNIX Programmers Manual.
'grep' means 'Global/Regular Expression/Print'.
See also egrep, fgrep
% grep -n 'write(6' *.f
list by filename and line number all occurrences of "write(6" in any
file that ends in .f in the current directory.
% grep -n 'text' file1 /dev/null
list filename (in this case, "file1") and line number of all occurrences
of 'text' in the file "file1" in the current directory. This is a
convenient way to force the filename to be listed, because there is
more than one file name specified AND /dev/null will never be listed.
% grep -i ^will /etc/passwd
force all comparisons to be case-independent and list all occurrences of the
string "will" beginning in column 1 which occur in the file /etc/passwd.
% grep -i -l regular *83*
force case-independence (-i), list only filename (-l), of any file
containing the string "regular" for any file in the current directory
which contains the string "83" in its name.
% grep -v backwards x
list all lines in file "x" which do NOT contain the string "backwards".
% grep "\\" file1
list all occurrences of the \ character which occur in file1.
% grep 'pegasus\!jones' addressfile
list occurrences of 'pegasus!jones' in the file addressfile. The backslash
is required to force csh to overlook the ! character.
% grep am.le /usr/dict/words
grep will match any pattern you give it, wherever it appears within
a line (or word) of the file /usr/dict/words. Therefore this will match
'amble' and 'ample', because they match the specification exactly,
AND 'amulet' and 'bramble', because the pattern exists within the words.
% grep -w am.le /usr/dict/words
the -w switch indicates to grep that it should print only a word which
matches in its entirety. Therefore this will print only five-character
words such as 'amble' or 'ample'.
From: henry@utzoo.UUCP (Henry Spencer) Organization: U of Toronto Zoology
...it is well known that fgrep is slow when searching for a single string.
...egrep ...is well known to be the fastest for the trivial case.
Where fgrep makes a difference is when you want to search for a number of
strings simultaneously. Grep can't do that at all, and egrep hits
pattern-complexity limits quickly. Fgrep really screams along in this case....
From: Mike Bloomberg
*** Regular expressions in Unix ***
Theme and Variations
A regular expression can be considered to be a string of characters
with certain characters having special meanings as defined below. These
special characters enable patterns to be defined in an efficient and
general manner.
The grep family (consisting of grep, egrep for expressions, and fgrep
for fixed strings), as well as awk, ed and sed make heavy use of
regular expressions. Greps search for the defined regular expression
within the input text file reporting any occurrences found.
Awk is a pattern processing language and is a generalization of grep.
Awk uses regular expressions, which are enclosed in slashes (\), to
locate the lines in the text file to perform actions upon.
ed and sed are line editors. Ed and ex interact with the user while sed
(stream editor) works in a "non- interactive" mode. Both processors
use regular expressions as context addresses. A context address is
the absolute position determined by the next location of the character
string that the regular expression matches within the text file.
These special characters are:
. (period)
Matches any single character (wildcard character)
EXAMPLE: the expression un.x will correspond to where
the third character can be ANY character. So, unax,
unbx, .... unzx, un3x, un&x, un;x etc. will all match.
-----------
[] Any one of the characters or range of characters within
these square brackets will match.
EXAMPLE: un[a3z]x will match unax or un3x or unzx only.
EXAMPLE: un[a-z]x will match unax, unbx ... unzx.
However, un3x will NOT match.
NOTE: placing a ^ (caret) preceding a group of
characters will match the COMPLEMENT of those letters.
EXAMPLE: un[^abcde] will match unfx ... unzx un1x ...
un9x un!x un$x etc.
-----------
() Used for grouping of an expression. The expression
enclosed in the parentheses can be operated by such
operators as the * or + operators.
EXAMPLE: (unix)+ would match unix or unixunix or
unixunixunix etc.
NOTE: Only for egrep,awk,ed,sed
-----------
| An "or" conditional. Matches EITHER of the expressions
to the left or right of the | (vertical bar) symbol.
EXAMPLE: unix|grep will find the line that has either
(or both) unix OR grep in it.
NOTE: Only for egrep,awk,ed,sed
-----------
^ (caret)
Placed at the beginning of the expression, means to
match the expression ONLY if it is at the beginning of
the line (column 1). Otherwise, the ^ is taken as a literal.
EXAMPLE: ^unix will match only if the line begins with
the word unix
-----------
$ (dollar sign)
Placed at the end of the expression means to match the
expression ONLY if it is at the end of the line.
Otherwise, the $ is taken as a literal.
EXAMPLE: unix$ will match only if the line ends with the
word unix.
-----------
\ Disables the special characters.
EXAMPLE: \. would look for a period in the text.
\\ would look for a backslash in the text.
===========================
The following symbols apply to the character
immediately PRECEDING the symbol.
===========================
* (asterisk)
Matches on any number (including zero) of occurrences of
the character immediately preceding.
EXAMPLE: un*x would match ux, unx, unnx, unnnx etc.
-----------
+ Similiar to * but matches one or more occurrences.
EXAMPLE: un+x would match unx, unnx, unnnx, unnnnx etc.
-----------
\< Matches expression that follows anything but a letter,
digit or underscore. Normally used to find expressions
at the front of the word.
EXAMPLE: \ Matches expression that precedes anything but a letter,
digit or underscore. Normally used to find expressions
at the end of the word.
EXAMPLE: abc\> will find all words ending with the
letters abc
NOTE: Only for grep,egrep,fgrep
-----------
\( and \)
Enclosing an expression with a "\(" on the left and a
"\)" on the right makes it referable later in the
expression by the syntax "\n". "n" is the numeric order
of the enclosed expression.
EXAMPLE: \(abc\)def\1 will match the string abcdefabc
EXAMPLE: \(abc\)\(def\)ghi\2\1 would match
abcdefghidefabc
NOTE: Only for ex,ed,sed
======================
For more documentation about regular expressions, see the "Unix
Programmer's Manual, Seventh Edition, November 1980, Computer Science
Division, Univ. of California at Berkeley" which contains hardcopy
version of "man" description of processors.