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 ) must be preceeded by \ to ENABLE 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
Invert the sense of matching to
select non-matching lines.
-e pattern
Use pattern as the matching pattern;
useful to protect patterns beginning with - from being intrepreted as options.
-f file
Obtain patterns from file, one per line. An empty file contains zero patterns and matches nothing.
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.

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

controlling output
only the matching part of the line is output.
when may be never, always, or auto

The matched sections can be displayed in color as per $GREP_COLOR in the form ff[;bb]
where ff is the code used for the foreground and bb is used for the background.

magenta on yellow 35;43 blue on cyan 34;46 red 31 (default)
Foreground: black 30, green 32, yellow 33, blue 34, magenta 35, cyan 36, white 37, red 31 (default)
Background: black 40, green 42, yellow 43, blue 44, magenta 45, underscore 4, [blink] 5, inverse 7
export GREP_COLOR="33;44"

Codes are not output when inappropriate (For example: to a file or pipe) unless when=always

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

output num lines of context before and after match
-B num
Output num lines of context before lines containing the pattern
then the lines containing pattern.
-A num
Output num lines of context after outputting the pattern.
Places a line containing -- between sets of matches.
   grep -C1 day  grep -B2 day  grep -A2 day
 input  output
mon mon
yesterday yesterday yesterday
day day day day
later later later
------set seperator
mon2 mon2
yr2 yr2 yr2
day2 day2 day2 day2
later2 later2 later2
f f

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$ #dont show lines with 0 count
Prefix each line with the line number
prefix each line with the byte offset within the input file
Use Unix-style byte offsets.
report byte offsets i.e. with CR characters stripped off. only for MS-DOS and MS-Windows.
Quiet; do not write to STDOUT.
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.
Suppress error messages about nonexistent or unaccessable files normally sent to STDERR.
filename is prepended to each match when multiple files are searched.
outputs the only the filename of files containing a match.
Once a match is found proceeds to the next file.
outputs the filenname with no match
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. Useful for tools like zgrep, e.g.
gzip -cd foo.gz |grep --label=foo something

-m num
--max-count= num
stop 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, enables calling process to resume a search.


> while grep -B 2 -m 1 bod; do echo ; done
Outputs any trailing context lines.
With -c or --count , does not output a count greater than num.

With -v or --invert-match , stops after outputting num non-matching lines.

Directory processing
-d action
If an input is a directory:
action: read: as ordinary files, default.
         recurse: reads all files under each directory, recursively.
equivalent to -d recurse .
--include=pattern Recurse only searching file matching pattern.
--exclude=pattern Recurse skip file matching pattern.

Special handling
--binary‑files=type If the first few bytes of a file indicate it contains binary data (non-ASCII text),
outputs either a one-line message saying that a binary file matches, or no message if there is no match.

binary and without-match assume binary file does not match, default.
text processe a binary file as if it were ascii text; equivalent to -a .

Note: Control characters sent to the terminal can set attributes making it unreadable. If this happens try STTY SANE

Process a binary file as if it were ASCII text
‑‑binary‑files=text equivalent.
-I Ignore a binary file (treat it as if it did not contain matching data )
‑‑binary‑files=without‑match equivalent.
Process 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 .
Display the version to standard error.
--mmap use the mmap system call to read input, instead of read which may yield better performance.
May cause undefined behavior if an input file shrinks, or if an I/O error occurs.
-D action
If an input file is a device, FIFO or socket, use action to process it.
read, which means that devices are read as ordinary files (default).
skip, devices are silently skipped.
--line‑buffered can have a performance penality.

Basic Regular Expressions: ? , + , { , | , ( and ) alone have no special meaning
   Use backslash to ENABLE special meaning (ex: \?)
   * is a meta character.

Regular Expressions

A pattern describes a set of strings, using operators to combine smaller expressions.


Grep understands two different versions of regular expression syntax: basic and extended.
GNU grep, there is no difference.

The simplest regular expression matchs a single character.  For example: a matches an a , R matches an R
Regular expresions can be combined.  For example: AR matches AR

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

These can be treated as normal character by preceding it with a \  (backslash) (the opposite of BRE)!

.  (period) matches any single character.
^  (caret) matches the beginning of a line
$  (dollar sign) matches the end of a line.

repetition operators may follow a regular expression.

preceeding item is matched
? at most once (more)

* 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

For stingy matching:
a question mark after any of the greedy quantifiers, chooses the smallest quantity for the first try.
/.*foo/ matches BOTH words in barfoo stoolfoo .
/.*i?foo/ matches the FIRST word in barfoo stoolfoo

+ one or more times
{n} exactly n times
{n,} n or more times
{n,m} at least n times, but not more than m times.

The braces may need to be escaped as in z\{1,3\} means: z zz or zzz

/9\{2,3\} bytes/ matches 99 bytes and 999 bytes
\> matches the empty string at the beginning of a word,    \< … 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.

  • bracket expressions match a single character.
    • list of characters enclosed by [ and ] matches any single character in the list
      Most metacharacters have no special meaning inside lists.
       To include ] place it first in the list. ][)(
       To include ^ place it anywhere but first. Otherwise it matches the beginning of the line.
       To include - place it last, lest it be considered a range
      For example: [12345689] matches any single digit except 0 or 7.
    • nonmatching list
      ^  (caret) preceeding the expression matchs any character not in the list.
      For example:
      ^[abcdefghijklmnopqrstuvwxyz] matches a single non-lowercase character.
    • range expression
      - (hyphen) seperating two characters matches any single character that sorts between the two characters, inclusive,
      using the locale's collating sequence and character set
      For example:
      [3-7] is equivalent to [34567].
      Many locales sort characters in dictionary order, in these locales [a-d] is typically equivalent [aBbCcDd] not [abcd].
      To obtain the traditional interpretation of bracket expressions ( where [a-d] is equivalent to [abcd]), use the C locale by setting the LC_ALL environment variable to the value C.
  • named classes a single character
    [:alnum:], [:alpha:], [:cntrl:], [:digit:], [:graph:], [:lower:], [:print:], [:punct:], [:space:], [:upper:], and [:xdigit:].
    Brackets in these class names are part of the symbolic names, and must be included in addition to any brackets that may be delimiting a bracket expression.
    [[:digit:]] is equivalent to [0123456789]
    [[:alnum:]] is frequently equivalent to [0-9A-Za-z] depending on the C locale and the ASCII character encoding.
    Whereas the [:alnum:] is independent of locale and character set.

    \w (word) is a synonym for [[:alnum:]]and
    \W is a synonym for [^[:alnum:]].

subexpression is defined using \(…\)

backreference \n, where n is a single digit, matches the substring previously matched by the nth subexpression
Frequently on the right hand side of a substitute command

Precedence: Repetition, concatenation, alternation.
A subexpression overrides precedence rules.

Environment Variables

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

GREP_COLOR Specifies the color for highlighting.

The 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 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.


 0: selected lines WERE found
 1: no lines selected. if [ $? != 1 ];then …
 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 grep

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


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

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'

To find an A surrounded by tabs, using ANSI-C quoting for bash use:
grep $'\tA\t'

Change a line beginning with hours:minutes (where hours may be one or two digits) to be zero filled to two digits if it was only one digit:
Green defines a subexpression, brown uses the subexpression
> echo "8:27 stuff stuff" |sed "s/^\(.\):/0\1:/" # Take first any character, folllowed by a colon, substitute a 0 before it and a colon
08:27 stuff stuff

> echo "19:42 stuff stuff" |sed "s/^\(.\):/0\1:/"
19:42 stuff stuff
# no change as there are 2 characters between the start of the line and the colon

Environment Variables
See environ for the following environment variables : LC_COLLATE, LC_CTYPE, LC_MESSAGES, and NLSPATH.


If there is a line with embedded nulls, grep will only compare up to the first null; if it matches, it will display the entire line.

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

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.

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