grep command

Updated 29 October 2024

Global search Regular Expression Print

Usage

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

Description

grep searches each FILE for PATTERNS. PATTERNS is a string that contains one or more patterns separated by newline characters, and grep prints each line that matches a pattern. When using grep in a shell command, PATTERNS should usually be quoted.

A FILE of "-" denotes standard input. If no FILE is specified, recursive searches examine the working directory, while non-recursive searches read standard input.

Furthermore, the variant programs egrep, fgrep, and rgrep are equivalent to grep -E, grep -F, and grep -r, respectively. These variants are obsolete, but remain available for backward compatibility.

Characters

CharacterDescription
[A-Z­a-z]Any lower and upper case letter.
[0-9]Any number.
[0-9­A-Z­a-z] Any lower and upper case letter or digit.

Posix meta-characters

Character classesDescription
[:alpha:]Match any alphabetical character, either upper or lower case
[:alnum:]Matches any alphanumeric character 0–9, A–Z, or a–z
[[:blank:]]Matches a space or Tab character
[:digit:]Matches a numerical digit from 0 through 9
[[:lower:]]Matches any lowercase alphabetical character a–z
[[:print:]]Matches any printable character
[[:punct:]]Matches a punctuation character
[[:upper:]]Matches any uppercase alphabetical character A–Z
[[:space:]]Matches any whitespace character: space, Tab, NL (newline), FF (formfeed), VT (vertical tab), CR (carriage return)

BRE, ERE & PCRE

Basic Regular Expressions (BRE)

Unless they are preceded by a backslash, these characters have a special meaning in BRE.: ^ $ . * [ ] \

However, unless they are preceded by a backslash, these characters have no special meaning.:

? + { } | ( )

Extended Regular Expressions (ERE)

Unless they are escaped with a backslash, ERE gives each of these characters a unique meaning:

^ $ . * + ? [ ] ( ) | { }

Wildcards

Wildcard CharacterDescription
.Match any Character Except New Line
?Match 0 or One characters
*Match 0 or More characters
+Match 1 or More characters

Quantifiers

Quantifier Description
{n}Matches exactly n times
{n,}Matches n or More
{,m} matches up to m (maximum)
{n,m} Matches between n and m (Minimum, Maximum)

Position

^Beginning of line.
$End of line.
^$Empty line.
\<Start of word.
\>End of word.

Options Examples

grep ‘alias’ filenamefind all occurrences of a string (basic usage)find all occurrences of a string (basic usage)
-cgrep -c ‘error’ /var/log/syslogcount the number of matches.
-egrep -e ‘linux$’ filenameuse regex (lines ending with 'Linux')
-fgrep -f pattern_file .bashrctake PATTERNS from FILE
-igrep -i ‘Alias’ .bashrcignore case search
-lgrep -l ‘alias’ /home/traw/*returns files with matches
-Lgrep -L ‘alias’ /home/traw/*returns files without matches
-rgrep -r ‘error’ /var/log/log.txtrecursive search (within subdirs)
-vgrep -v ‘warning’ /home/traw/log.txtselect non-matching lines
-mgrep -m 3 ‘alias’ .bashrclimit grep Output upto 3 lines.
-ngrep -n ‘sudo’ .bashrcprint line numbers of the matches.
-ogrep -o ‘search string’ .bashrconly show the matching part of the string.
-xgrep -x ‘linux is love’ filenamematch only whole lines
-wgrep -w "alias" .bashrcmatch only whole words
-Agrep -A 6 ‘error’ error.logprint 6 lines after a match
-Bgrep -B 2 ‘warning’ error.logprint 2 lines before a match
-Cgrep -C 7 ‘info’ error.logprint 7 lines before and after a match
-Egrep -E ‘b(a|e)g’ filenameExtended regex (lines containing bag or bag)

Perl Compatible Regular Expressions (PCRE)

Additional anchors and character classes, lookahead/lookbehind, conditional expressions, comments, and other features are available in PCRE.

Spotted a mistake or want something added? Send me a note.