man > egrep

πŸ” grep, egrep, fgrep, rgrep – print lines that match patterns

πŸ“– NAME

grep, egrep, fgrep, rgrep β€” print lines that match patterns

πŸš€ Quick Reference

Use Case Command Description
πŸ” Basic search grep 'pattern' file Print lines matching pattern in a file.
πŸ” Case-insensitive grep -i 'pattern' file Ignore case when matching.
πŸ” Recursive search grep -r 'pattern' dir/ Search recursively through directories.
πŸ” Invert match grep -v 'pattern' file Print lines that do NOT match.
πŸ” Count matches grep -c 'pattern' file Print count of matching lines per file.
πŸ” Show line numbers grep -n 'pattern' file Prefix each output line with its line number.
πŸ” Only matching text grep -o 'pattern' file Print only the matched parts of a line.
πŸ” Context lines grep -C 3 'pattern' file Show 3 lines of context before and after match.
πŸ” Fixed strings (no regex) grep -F 'literal' file Treat pattern as fixed string, not regex.
πŸ” Extended regex (ERE) grep -E 'pattern' file Use extended regular expressions.
πŸ” Perl-compatible regex (PCRE) grep -P 'pattern' file Use Perl-compatible regular expressions.
πŸ” Binary files as text grep -a 'pattern' file Treat binary file as text.
πŸ” Max count grep -m 5 'pattern' file Stop after finding 5 matches.
πŸ” Quiet (silent) grep -q 'pattern' file Exit immediately if match found (no output).
πŸ” Exclude files grep --exclude='*.log' 'pattern' . Skip files matching glob pattern.

πŸ”§ SYNOPSIS

grep [OPTION]... PATTERNS [FILE]...
grep [OPTION]... -e PATTERNS ... [FILE]...
grep [OPTION]... -f PATTERN_FILE ... [FILE]...

πŸ“ DESCRIPTION

grep searches for PATTERNS in each FILE. PATTERNS is one or more patterns separated by newline characters, and grep prints each line that matches a pattern. Typically PATTERNS should be quoted when grep is used in a shell command.

A FILE of β€œ-” stands for standard input. If no FILE is given, recursive searches examine the working directory, and nonrecursive searches read standard input.

In addition, the variant programs egrep, fgrep and rgrep are the same as grep -E, grep -F, and grep -r, respectively. These variants are deprecated, but are provided for backward compatibility.

βš™οΈ OPTIONS

ℹ️ Generic Program Information

πŸ”€ Pattern Syntax

🎯 Matching Control

πŸ“„ General Output Control

🏷️ Output Line Prefix Control

πŸ“ Context Line Control

πŸ“ File and Directory Selection

πŸ› οΈ Other Options

πŸ” REGULAR EXPRESSIONS

A regular expression is a pattern that describes a set of strings. Regular expressions are constructed analogously to arithmetic expressions, by using various operators to combine smaller expressions.

grep understands three different versions of regular expression syntax: β€œbasic” (BRE), β€œextended” (ERE) and β€œperl” (PCRE). In GNU grep there is no difference in available functionality between basic and extended syntaxes. In other implementations, basic regular expressions are less powerful. The following description applies to extended regular expressions; differences for basic regular expressions are summarized afterwards. Perl-compatible regular expressions give additional functionality, and are documented in pcresyntax(3) and pcrepattern(3), but work only if PCRE support is enabled.

The fundamental building blocks are the regular expressions that match a single character. Most characters, including all letters and digits, are regular expressions that match themselves. Any meta-character with special meaning may be quoted by preceding it with a backslash.

The period . matches any single character. It is unspecified whether it matches an encoding error.

πŸ“‹ Character Classes and Bracket Expressions

A bracket expression is a list of characters enclosed by [ and ]. It matches any single character in that list. If the first character of the list is the caret ^ then it matches any character not in the list. For example, the regular expression [0123456789] matches any single digit.

Within a bracket expression, a range expression consists of two characters separated by a hyphen. It matches any single character that sorts between the two characters, inclusive, using the locale's collating sequence and character set. For example, in the default C locale, [a-d] is equivalent to [abcd]. Many locales sort characters in dictionary order, and in these locales [a-d] is typically not equivalent to [abcd]; it might be equivalent to [aBbCcDd], for example. To obtain the traditional interpretation of bracket expressions, you can use the C locale by setting the LC_ALL environment variable to the value C.

Finally, certain named classes of characters are predefined within bracket expressions, as follows: [:alnum:], [:alpha:], [:blank:], [:cntrl:], [:digit:], [:graph:], [:lower:], [:print:], [:punct:], [:space:], [:upper:], and [:xdigit:]. For example, [[:alnum:]] means the character class of numbers and letters in the current locale. In the C locale and ASCII character set encoding, this is the same as [0-9A-Za-z]. Most meta-characters lose their special meaning inside bracket expressions. To include a literal ] place it first in the list. Similarly, to include a literal ^ place it anywhere but first. Finally, to include a literal - place it last.

πŸ“ Anchoring

The caret ^ and the dollar sign $ are meta-characters that respectively match the empty string at the beginning and end of a line.

πŸ”— The Backslash Character and Special Expressions

The symbols \< and \> respectively match the empty string at the beginning and end of a word. The symbol \b matches the empty string at the edge of a word, and \B matches the empty string provided it's not at the edge of a word. The symbol \w is a synonym for [_[:alnum:]] and \W is a synonym for [^_[:alnum:]].

πŸ” Repetition

A regular expression may be followed by one of several repetition operators:

πŸ”— Concatenation

Two regular expressions may be concatenated; the resulting regular expression matches any string formed by concatenating two substrings that respectively match the concatenated expressions.

πŸ”€ Alternation

Two regular expressions may be joined by the infix operator |; the resulting regular expression matches any string matching either alternate expression.

πŸ”’ Precedence

Repetition takes precedence over concatenation, which in turn takes precedence over alternation. A whole expression may be enclosed in parentheses to override these precedence rules and form a subexpression.

πŸ”™ Back-references and Subexpressions

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

πŸ“Š Basic vs Extended Regular Expressions

In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their special meaning; instead use the backslashed versions \?, \+, \{, \|, \(, and \).

πŸšͺ Exit Status

Normally the exit status is:

However, if the -q or --quiet or --silent is used and a line is selected, the exit status is 0 even if an error occurred.

🌐 ENVIRONMENT

The behavior of grep is affected by the following environment variables.

The locale for category LC_foo is specified by examining the three environment variables LC_ALL, LC_foo, LANG, in that 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 the Brazilian Portuguese locale is used for the LC_MESSAGES category. The C locale is used if none of these environment variables are set, if the locale catalog is not installed, or if grep was not compiled with national language support (NLS). The shell command locale -a lists locales that are currently available.

🎨 GREP_COLOR

This variable specifies the color used to highlight matched (non-empty) text. It is deprecated in favor of GREP_COLORS, but still supported. The mt, ms, and mc capabilities of GREP_COLORS have priority over it. It can only specify the color used to highlight the matching non-empty text in any matching line. The default is 01;31, which means a bold red foreground text on the terminal's default background.

🎨 GREP_COLORS

Specifies the colors and other attributes used to highlight various parts of the output. Its value is a colon-separated list of capabilities that defaults to ms=01;31:mc=01;31:sl=:cx=:fn=35:ln=32:bn=32:se=36 with the rv and ne boolean capabilities omitted (i.e., false). Supported capabilities are as follows:

Note that boolean capabilities have no =... part. They are omitted (i.e., false) by default and become true when specified.

🌍 LC_ALL, LC_COLLATE, LANG

These variables specify the locale for the LC_COLLATE category, which determines the collating sequence used to interpret range expressions like [a-z].

🌍 LC_ALL, LC_CTYPE, LANG

These variables specify the locale for the LC_CTYPE category, which determines the type of characters, e.g., which characters are whitespace. This category also determines the character encoding.

🌍 LC_ALL, LC_MESSAGES, LANG

These variables specify the locale for the LC_MESSAGES category, which determines the language that grep uses for messages.

βš™οΈ POSIXLY_CORRECT

If set, grep behaves as POSIX requires; otherwise, grep behaves more like other GNU programs. POSIX requires that options that follow file names must be treated as file names; by default, such options are permuted to the front of the operand list and are treated as options. Also, POSIX requires that unrecognized options be diagnosed as β€œillegal”, but the default is to diagnose them as β€œinvalid”. POSIXLY_CORRECT also disables _N_GNU_nonoption_argv_flags_, described below.

βš™οΈ _N_GNU_nonoption_argv_flags_

(Here 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 of grep to be an option, even if it appears to be one. This behavior is available only with the GNU C library, and only when POSIXLY_CORRECT is not set.

πŸ“ NOTES

This man page is maintained only fitfully; the full documentation is often more up-to-date.

©️ COPYRIGHT

Copyright 1998-2000, 2002, 2005-2021 Free Software Foundation, Inc.

This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

πŸ› BUGS

πŸ“§ Reporting Bugs

Email bug reports to the bug-reporting address bug-grep AT gnu.org. An email archive https://lists.gnu.org/mailman/listinfo/bug-grep and a bug tracker https://debbugs.gnu.org/cgi/pkgreport.cgi?package=grep are available.

⚠️ Known Bugs

πŸ’‘ EXAMPLE

The following example outputs the location and contents of any line containing β€œf” and ending in β€œ.c”, within all files in the current directory whose names contain β€œg” and end in β€œ.h”. The -n option outputs line numbers, the -- argument treats expansions of β€œ*g*.h” starting with β€œ-” as file names not options, and the empty file /dev/null causes file names to be output even if only one file name happens to be of the form β€œ*g*.h”.

$ grep -n -- 'f.*\.c$' *g*.h /dev/null
argmatch.h:1:/* definitions and prototypes for argmatch.c

The only line that matches is line 1 of argmatch.h. Note that the regular expression syntax used in the pattern differs from the globbing syntax that the shell uses to match file names.

πŸ“š SEE ALSO

πŸ“„ Regular Manual Pages

awk(1), cmp(1), diff(1), find(1), perl(1), sed(1), sort(1), xargs(1), read(2), pcre(3), pcresyntax(3), pcrepattern(3), terminfo(5), glob(7), regex(7)

πŸ“– Full Documentation

A complete manual https://www.gnu.org/software/grep/manual/ is available. If the info and grep programs are properly installed at your site, the command

info grep

should give you access to the complete manual.

egrep
πŸ” grep, egrep, fgrep, rgrep – print lines that match patterns πŸ“– NAME πŸš€ Quick Reference πŸ”§ SYNOPSIS πŸ“ DESCRIPTION βš™οΈ OPTIONS
ℹ️ Generic Program Information πŸ”€ Pattern Syntax 🎯 Matching Control πŸ“„ General Output Control 🏷️ Output Line Prefix Control πŸ“ Context Line Control πŸ“ File and Directory Selection πŸ› οΈ Other Options
πŸ” REGULAR EXPRESSIONS
πŸ“‹ Character Classes and Bracket Expressions πŸ“ Anchoring πŸ”— The Backslash Character and Special Expressions πŸ” Repetition πŸ”— Concatenation πŸ”€ Alternation πŸ”’ Precedence πŸ”™ Back-references and Subexpressions πŸ“Š Basic vs Extended Regular Expressions
πŸšͺ Exit Status 🌐 ENVIRONMENT
🎨 GREP_COLOR 🎨 GREP_COLORS 🌍 LC_ALL, LC_COLLATE, LANG 🌍 LC_ALL, LC_CTYPE, LANG 🌍 LC_ALL, LC_MESSAGES, LANG βš™οΈ POSIXLY_CORRECT βš™οΈ _N_GNU_nonoption_argv_flags_
πŸ“ NOTES ©️ COPYRIGHT πŸ› BUGS
πŸ“§ Reporting Bugs ⚠️ Known Bugs
πŸ’‘ EXAMPLE πŸ“š SEE ALSO
πŸ“„ Regular Manual Pages πŸ“– Full Documentation

Generated by phpman v4.10.0-7-g98e9fd5 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-09-13 12:07 @216.73.216.143
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!

^_top_^