Global search Regular Expression Print

Usage

$ 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

Character Description
[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 classes Description
[: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 Character Description
. 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’ filename find all occurrences of a string (basic usage)find all occurrences of a string (basic usage)
-c grep -c ‘error’ /var/log/syslog count the number of matches.
-e grep -e ‘linux$’ filename use regex (lines ending with 'Linux')
-f grep -f pattern_file .bashrc take PATTERNS from FILE
-i grep -i ‘Alias’ .bashrc ignore case search
-l grep -l ‘alias’ /home/traw/* returns files with matches
-L grep -L ‘alias’ /home/traw/* returns files without matches
-r grep -r ‘error’ /var/log/log.txt recursive search (within subdirs)
-v grep -v ‘warning’ /home/traw/log.txt select non-matching lines
-m grep -m 3 ‘alias’ .bashrc limit grep Output upto 3 lines.
-n grep -n ‘sudo’ .bashrc print line numbers of the matches.
-o grep -o ‘search string’ .bashrc only show the matching part of the string.
-x grep -x ‘linux is love’ filename match only whole lines
-w grep -w "alias" .bashrc match only whole words
-A grep -A 6 ‘error’ error.log print 6 lines after a match
-B grep -B 2 ‘warning’ error.log print 2 lines before a match
-C grep -C 7 ‘info’ error.log print 7 lines before and after a match
-E grep -E ‘b(a e)g’ filename

Perl Compatible Regular Expressions (PCRE)

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