grep [options] pattern [file...]
grep [options] [-e pattern | -f patternFile] [file...]

Searches the input files (or STDIN if no files are named, or if file is - )
for lines containing a match to the pattern.

-G --basic-regexp pattern is a Basic Regular Expression (default).
                                                  ?, +, {, |, ( and ) have no special meaning

-E --extended-regexp pattern is an Extended Regular Expression.
egrep is the same as grep -E.

-F --fixed-strings pattern is a list of fixed strings, separated by newlines, any of which is to be matched.
fgrep is the same as grep -F.

-P --perl-regexp Interpret pattern as a Perl regular expression.

controling matching
-v
--invert-match
Invert the sense of matching to select non-matching lines.
-i
--ignore-case
 
-e pattern
--regexp=pattern
Use pattern as the matching pattern; useful to protect patterns beginning with - from being intrepreted as options.
-f file
--file=file
Obtain patterns from file, one per line.
An empty file contains zero patterns and matches nothing.
-w
--word-regexp
matches must form whole words.

The matching substring must either be at the beginning of the line, or preceded by a non-alphanumeric character and
must be either at the end of the line or followed by a non-alphanumeric character.

Alphanumeric characters are letters, digits, and the underscore.

-x
--line-regexp
exactly match the whole line.
-y Obsolete synonym for -i (--ignore-case).

controlling output
-o
--only-matching
Show only the part of a line that matches pattern.
--color[=when]
-colour[=when]
Display matches with screen attributes as per the GREP_COLOR environment variable.
Foreground: black 30, green 32, yellow 33, blue 34, purple 35, cyan 36, white 37, red 31 (default)
Background: black 40, green 42, yellow 43, blue 44, purple 45, underscore 4, [blink] 5, inverse 7
codes can be combined using ; as in export GREP_COLOR="33;44"

when may be never, always, or auto

Codes are not output when inapropriate (like going to a file or pipe) unless when=always

grep --color=always birds logfile | more -r

-Bnum
--before-context=num
Output num lines of context before lines containing the pattern
then the lines containing pattern.
-Anum
--after-context=num
Output lines containing the pattern and num lines of context after them.
Places a line containing -- between contiguous groups of matches.
example:
   grep -A2 d  grep -B2 d
 input  output
a
b
c
 
b
c
d d d
e
f
g
e
f
 
----
aa
bb
cc
 
bb
cc
dd dd dd
ee
ff
gg
ee
ff

-c
--count
only output count of matches (or -cv non-matches) for each input file
Prefixed by 'filename :' if multiple files, even if count is 0
grep -c images *html |grep -v 0$
     With the -v, --invert-match, count non-matching lines.
-n
--line-number
Prefix each line with the line number
-b
--byte-offset
display the byte offset within the input file before each line
-u
--unix-byte-offsets
Use Unix-style byte offsets. report byte offsets i.e. with CR characters stripped off. only for MS-DOS and MS-Windows.
-q
--quiet
--silent
Quiet; do not write to standard output.
Errors like "Permission denied" are written to STDERR and return code is set to 2.
Exit with zero status as soon as a match is found.
If no match is found $? is set to 1.
-s
--no-messages
Suppress error messages about nonexistent or unaccessable files normally sent to STDERR.
-H
--with-filename
filename is prepended to each match when multiple files are searched.
-h
--no-filename
don't
-l
--files-with-matches
outputs the only the filename of files containing a match.
Once a match is found grep proceeds to the next file.
-L
--files-without-match
outputs the filenname with no match
-Z
--null
terminate filename with NULL . Used when file names contain newlines!.

Used with commands find -print0, perl -0, sort -z, and xargs -0.

--label=label Displays input actually coming from standard input as input coming from file label. This is useful for tools like zgrep, e.g.
gzip -cd foo.gz |grep --label=foo something

Restricting Processing
-C num
--context=num
limit output to num lines
-m num
--max-count= num
stop reading a file after num matching lines.
If the input is standard input from a regular file, and num matching lines are output, the standard input is positioned to just after the last matching line. This enables a calling process to resume a search.

When grep stops after num matching lines, it outputs any trailing context lines.
When used with -c or --count , grep does not output a count greater than num.

When used with -v or --invert-match , grep stops after outputting num non-matching lines.

Directory processing
-R
-r
--recursive
Read all files under each directory, recursively; equivalent to -d recurse .
-d action
--directories=action
If an input is a directory, use action to process it.
recurse read all files under each directory, recursively; equivalent to -r .
--include=pattern Recurse in directories only searching file matching pattern.
--exclude=pattern Recurse in directories skip file matching pattern.

Special handling
--binary‑files=type If the first few bytes of a file indicate that the file contains binary data, outputs either a one-line message saying that a binary file matches,
or no message if there is no match.
binary
the default and without-match, grep assumes that a binary file does not match; this is equivalent to -I (Ignore).
text
processe a binary file as if it were ascii text; equivalent to -a .
Outputing control characters to the terminal can set attributes on the terminal making it unreadable. If this happens try STTY SANE
-a
--text
Process a binary file as if it were ASCII text
equivalent to ‑‑binary‑files=text.
-I Ignore a binary file (treat it as if it did not contain matching data )
equivalent to ‑‑binary‑files=without‑match.
-U
--binary
Treat the file(s) as binary.
By default, under MS-DOS and MS-Windows, grep guesses the file type by looking at the contents of the first 32KB read from the file. If grep decides the file is a text file, it strips the CR characters from the original file contents (to make regular expressions with ^ and $ work correctly).
Specifying -U causes all files to be read and passed to the matching mechanism verbatim; if the file is a text file with CR/LF pairs at the end of each line, this will cause some regular expressions to fail. only effective under MS-DOS and MS-Windows unless used with -b .
--help
-V
--version
Display the version to standard error.
--mmap use the mmap system call to read input, instead of read which may yield better performance. This can cause undefined behavior if an input file shrinks, or if an I/O error occurs.
-D action
--devices=action
    
If an input file is a device, FIFO or socket, use action to process it.
The default action is read, which means that devices are read as ordinary files.
If action is skip, devices are silently skipped.
--line-buffered can have a performance penality.

Basic Regular Expressions: ?, +, {, |, ( and ) have no special meaning
* is a meta character.

REGULAR EXPRESSIONS

A pattern that describes a set of strings, constructed by using various operators to combine smaller expressions.

Grep understands two different versions of regular expression syntax: basic and extended.
In GNU grep, there is no difference in available functionality using either syntax.

The simplest regular expression matchs a single character.

examples: a matches a , R matches R
Regular expresions can be combined. AR matches AR

.  (period) matches any single character.
^  (caret) matches the beginning of a line
$  (dollar sign) matches the end of a line.
\w (word) is a synonym for [[:alnum:]] and
\W is a synonym for [^[:alnum:]].
\> matches the empty string at the beginning of a word.
\< matches the empty string at the end of a word.
\b matches the empty string at the edge of a word,
\B matches the empty string provided it's not at the edge of a word.

repetition operators follow a regular expression


the preceding item
? matched at most once
Use for stingy vs greedy matching. placing a question mark after any of the greedy quantifiers, they can be made to choose the smallest quantity for the first try. So if you say /.*?foo/, the .*?
* will be matched zero or more times
Match is greedy attempting to match as much as possible. for example /.*xx/ matches everything in the line:asdfbxx asdfxx asdfxx
+ will be matched one or more times
{n} is matched exactly n times
The braces may need to be escaped as in z\{1,3\} z zz or zzz
{n,} is matched n or more times
{n,m} is matched at least n times, but not more than m times.

/[0-9]\{2,3\} bytes/ matches 99 bytes and 999 bytes
as does
/[[:digit:]]\{2,3\} bytes/

A metacharacter ( examples: * , . , [) can be treated as normal character by preceding it with a \  (backslash)

Two regular expressions may be concatenated as in grep "aa""bb".

Regular expressions joined by | match any string matching either expression.

Repetition takes precedence over concatenation, which in turn takes precedence over alternation.
A subexpression enclosed in parentheses overrides precedence rules.

backreference \n, where n is a single digit, matches the substring previously matched by the nth parenthesized subexpression of the regular expression.

Environment Variables

GREP_OPTIONS are options to be placed before any explicit options.
Option specifications are separated by whitespace.
A backslash escapes the next character, used to specify an option containing whitespace or a backslash.
GREP_OPTIONS='--color'

GREP_COLOR Specifies the color for highlighting.

A locale LC_xxx is specified by examining:
LC_ALL, LC_xxx, LANG, in order.
The first of these variables that is set specifies the locale.
For example, if LC_ALL is not set, but LC_MESSAGES is set to pt_BR, then BrazilianPortuguese is used.
The C locale is used if none of these are set, or if the locale catalog is not installed, or if grep was not compiled with national language support (NLS).
LC_ALL, LC_COLLATE,  LANG collating sequence used to interpret range expressions like [a-z].
LC_ALL, LC_CTYPE,    LANG type of characters, e.g., which characters are whitespace.
LC_ALL, LC_MESSAGES, LANG language for messages. The default C locale uses American English messages.

POSIXLY_CORRECT If set, grep behaves as POSIX.2 requires; otherwise, grep behaves more like other GNU programs.

_N_GNU_nonoption_argv_flags_ (N is grep's numeric process ID.)
If the ith character of this environment variable's value is 1,
do not consider the ith operand to be an option, even if it appears to be one.
A shell can put this variable in the environment for each command it runs, specifying which operands are the results of file name wildcard expansion and therefore should not be treated as options.
Only with the GNU C library, and only when POSIXLY_CORRECT is not set.

Diagnostics
0 if selected lines are found and
1 no lines selected.
2 an error occurred like: no permission or file not found.

Large repetition counts in the {n,m} construct may cause grep to use lots of memory. Certain obscure regular expressions require exponential time and space.

Backreferences are very slow, and require exponential time.

Current "official" GNU Doc

See also egrep, fgrep, sed, sh, attributes, environ, largefile, regex, regexp, XPG4

GNU Project 2002/01/22 GREP(1)



From SUN /usr/bin/grep [ -bchilnsvw ] limited-regular-expression [ filename ... ]

/usr/xpg4/bin/grep [ -E | -F ] [ -c | -l | -q ] [ -bhinsvwx* ] ... pattern [ file ... ] ... -e pattern_list ... [ -f pattern_file ] ...
... [ -e pattern_list ... ] -f pattern_file ...

searches the input for a pattern and may display lines that contain that pattern.

Normally, each line found is copied to standard output.
The file name is displayed before each line found if there is more than one input file.

grep uses limited regular expressions like those described on the regexp(5) manual page to match the patterns.

/usr/xpg4/bin/grep

    grep interprets pattern_list as a Basic Regular Expressionas described on regex(5) manual page, default.
    -E pattern_list as a full regular expression
    -F pattern_list as a fixed string.
grep uses a compact non-deterministic algorithm. which means??

Since characters $, *, [, ^, |, (, ), and \ used in the pattern_list are meaningful to the shell,
enclose the entire pattern_list in apostrophes.

OPTIONS

    -v reVerse the sense of the operation. i.e. display all lines not containing the pattern.
    -i ignore case
    -n Precede each line by its line number in the file (first line is 1). -c only display a count of the lines that contain the pattern or
    -cv count of lines not matching. -h hide the name of the file containing the matching
    line normally appended to that line when searching multiple files. -l display unique list of the names of files with matching lines, separated by \n characters.
    -s Suppress error messages about nonexistent or unreadable files.
    -w search for the expression as a word as if surrounded by \< and \>
    -b Precede each line by the block number on which it was found, useful in locating block numbers by context (first block is 0). for /usr/xpg4/bin/grep only aka egrep :
    -e pattern_list
    Specify one or more patterns to be used during the search for input.
    patterns in pattern_list are separated by a NEWLINE character.
    A null pattern is specified by two adjacent newline characters in pattern_list.
    Unless the -E or -F is also specified, each pattern will be treated as a basic regular expression.
    Multiple -e and -f options are accepted . All of the specified patterns are used when matching lines, but the order of evaluation is unspecified.

    -E Match using Full Regular Expressions.
    If any entire full regular expression pattern matches an input line, the line will be matched.
    A null full regular expression matches every line.
    Each pattern will be interpreted as a full regular expression as and \), and including:

    followed by + that matches one or more occurrences of the full regular expression.
    followed by ? that matches 0 or 1 occurrences of the full regular expression.
    separated by | or by a new-line that match strings that are matched by any of the expressions.
    May be enclosed in parentheses ( ) for grouping.
    The order of precedence of operators is [ ], then * ? +, then concatenation, then | and new-line.

    -f pattern_file
    Read one or more patterns from the file named by the path name pattern_file. patterns in pattern_file are terminated by a NEWLINE character.
    A null pattern is specified by an empty line in pattern_file.
    Unless -E or -F is also specified, each pattern will be treated as a basic regular expression.

    -F match using fixed strings.
    Treat each pattern specified as a string instead of a regular expression. If an input line contains any of the patterns as a contiguous sequence of bytes, the line will be matched.
    A null string matches every line. See fgrep(1)

    -q Quiet. Do not write anything to the standard output, regardless of matching lines.
    Exit with zero status if a line was selected.

    -x Consider only input lines that use all characters in
    the line to match an entire fixed string or regular expression to be matching lines.

    Operands

    file If no file operands are specified, the standard input will be used.

    pattern used during the search . (/usr/bin/grep )

    one or more patterns . treated as if it were specified as -epattern_list.

    USAGE

    The -epattern_list is useful when pattern_list begins with the hyphen and has the same effect as the pattern_list operand. Useful when it is more convenient to provide multiple patterns as separate arguments.

    Multiple -e and -f * options are accepted and grep will use all of the patterns. The order of evaluation is not specified.
    If a null string exists as a pattern, it will match every line)

    The -q * option provides a means of easily determining whether or not a pattern (or string) exists in a group of files. When searching several files, it provides a performance improvement (because it can quit as soon as it finds the first match) and requires less care by the user in choosing the set of files to supply as arguments (because it will exit zero if it finds a match even if grep detected an access or read error on earlier file operands).

    Examples

    Find all uses of the "Posix" ( -i ignoring case) in the file text.mm, and write lines with line numbers( -n ):
    grep -i -n posix text.mm

    Display line numbers( -n ) containg empty lines ( i.e where beginning is immediately followed by end of line)
    grep -n ^$ or grep -n -v .

    Display all lines containing strings abc or def or both :
    grep -E 'abc def' -or- grep -F 'abc def'

    Both of the following commands display all lines matching exactly abc or def:
    grep -E '^abc$ ^def$' -or- grep -F -x 'abc def'

    Exit status

    0 sucessfully found one or more matches .
    1 failed to find any matches.
    2 Syntax errors or inaccessible files (even if some matches were found).
    Environment Variables
    See environ for the following environment variables : LC_COLLATE, LC_CTYPE, LC_MESSAGES, and NLSPATH.

    Notes

    Large File Behavior.
    See largefile(5) for the description of the behavior of grep when encountering files greater than or equal to 2 Gbyte ( 2**31 bytes). Lines are limited only by the size of the available virtual memory. If there is a line with embedded nulls, grep will only match up to the first null; if it matches, it will display the entire line.

    The results are unspecified if input files contain lines longer than LINE_MAX bytes or contain binary data. LINE_MAX is defined in /usr/include/limits.h.

See egrep, fgrep, sed, sh, attributes(5), environ(5), largefile(5), regex(5), regexp(5), XPG4(5)