Linux - Common Regex Expressions for grep Cheat Sheet
This introductory guide serves as a handy reference for common regular expressions (regex) you can use with the grep
command. If you’re just starting out or need a quick refresh, this cheat sheet will make it easy to get the hang of the essential regex patterns that go hand in hand with grep
.
Basic Patterns
.
: Matches any single character- Example:
grep "h.t" file.txt
matches “hat”, “hot”, “hit”, etc.
- Example:
^
: Matches the start of a line- Example:
grep "^The" file.txt
matches lines starting with “The”
- Example:
$
: Matches the end of a line- Example:
grep "end$" file.txt
matches lines ending with “end”
- Example:
*
: Matches zero or more occurrences of the previous character- Example:
grep "ca*t" file.txt
matches “ct”, “cat”, “caat”, etc.
- Example:
Character Classes
[abc]
: Matches any single character in the set- Example:
grep "[aeiou]" file.txt
matches any vowel
- Example:
[^abc]
: Matches any single character not in the set- Example:
grep "[^0-9]" file.txt
matches any non-digit
- Example:
[a-z]
: Matches any single character in the range- Example:
grep "[A-Z]" file.txt
matches any uppercase letter
- Example:
[a-zA-Z]
: Matches any lowercase or uppercase letter- Example:
grep "[a-zA-Z]" file.txt
matches any letter
- Example:
Note: Character classes are case-sensitive unless used with the -i
flag.
Basic vs Extended Regular Expressions
- Use
grep -E
for extended features such as+
,?
, and|
. - For basic usage, use
grep
without the-E
flag, though it has more limited regex capabilities.
Extended Regex Features (use with grep -E)
+
: Matches one or more occurrences of the previous character- Example:
grep -E "ca+t" file.txt
matches “cat”, “caat”, but not “ct”
- Example:
?
: Matches zero or one occurrence of the previous character- Example:
grep -E "colou?r" file.txt
matches both “color” and “colour”
- Example:
|
: Alternation (OR)- Example:
grep -E "cat|dog" file.txt
matches lines containing either “cat” or “dog”
- Example:
Quantifiers
{n}
: Matches exactly n occurrences- Example:
grep -E "a{3}" file.txt
matches “aaa”
- Example:
{n,}
: Matches n or more occurrences- Example:
grep -E "a{2,}" file.txt
matches “aa”, “aaa”, etc.
- Example:
{n,m}
: Matches between n and m occurrences- Example:
grep -E "a{2,4}" file.txt
matches “aa”, “aaa”, “aaaa”
- Example:
Special Characters
\
: Escapes special characters- Example:
grep "\." file.txt
matches actual periods
- Example:
\b
: Matches word boundaries- Example:
grep "\bthe\b" file.txt
matches “the” but not “there” or “other”
- Example:
Practical Examples
- Find all IP addresses:
grep -E "\b([0-9]{1,3}\.){3}[0-9]{1,3}\b" file.txt
- Match email addresses:
grep -E "[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}" file.txt
- Find lines with dates (YYYY-MM-DD format):
grep -E "^[0-9]{4}-[0-9]{2}-[0-9]{2}" file.txt
- Match lines starting with “Error” or “Warning”:
grep -E "^(Error|Warning)" log.txt
- Find words with at least eight characters:
grep -E "\b[A-Za-z]{8,}\b" file.txt
- Match lines that do not contain “Error” (inverse match):
grep -v "Error" log.txt
- Case-insensitive search for the word “hello”:
grep -i "hello" file.txt
Backslash Characters
When using grep -E
or grep -P
(PCRE mode), the following backslash characters have special meanings:
\b
: Word boundary- Example:
grep -E "\bcat\b" file.txt
matches “cat” but not “category”
- Example:
\B
: Non-word boundary- Example:
grep -E "\Bcat\B" file.txt
matches “cat” in “concatenate” but not “cat” alone
- Example:
\d
: Any digit (equivalent to [0-9])- Example:
grep -E "\d{3}" file.txt
matches any three consecutive digits
- Example:
\D
: Any non-digit (equivalent to [^0-9])- Example:
grep -E "\D+" file.txt
matches one or more non-digit characters
- Example:
\s
: Any whitespace character (space, tab, newline)- Example:
grep -E "hello\sworld" file.txt
matches “hello world” with any whitespace between
- Example:
\S
: Any non-whitespace character- Example:
grep -E "\S{5}" file.txt
matches any five consecutive non-whitespace characters
- Example:
\w
: Any word character (alphanumeric + underscore, equivalent to [a-zA-Z0-9_])- Example:
grep -E "\w+" file.txt
matches one or more word characters
- Example:
\W
: Any non-word character- Example:
grep -E "\W" file.txt
matches any single non-word character
- Example:
\\
: Literal backslash- Example:
grep -E "\\" file.txt
matches a single backslash character
- Example:
ℹ️ Note: When using basic
grep
(without -E or -P), you may need to escape these special backslash characters with an additional backslash, like\\s
or\\d
.
Practical Examples with Backslash Characters
- Find words starting with “pre”:
grep -E "\bpre\w+" file.txt
- Match phone numbers in format (xxx) xxx-xxxx:
grep -E "\(\d{3}\)\s?\d{3}-\d{4}" file.txt
- Find lines with exactly 3 words:
grep -E "^\s*\S+\s+\S+\s+\S+\s*$" file.txt
- Match email addresses using \w:
grep -E "\b[\w.%+-]+@[\w.-]+\.[A-Za-z]{2,}\b" file.txt