Using grep with Regular Expressions on Linux and Unix Systems
This guide explains how to use the GNU grep tool on Linux and Unix to perform powerful text searches with basic and extended regular expressions, covering pattern matching, character classes, wildcards, logical operators, quantifiers, output formatting, and differences between grep and egrep.
Linux ships with the GNU version of grep , which supports extended regular expressions and is used to search for text anywhere on a server or workstation.
1. Quick Overview of Regular Expressions
Regular expressions are patterns that match each input line. Examples include ^w1 , w1|w2 , [^ ] .
Search for the string vivek in /etc/passwd :
grep vivek /etc/passwd
Sample output:
vivek:x:1000:1000:Vivek Gite,,,:/home/vivek:/bin/bash vivekgite:x:1001:1001::/home/vivekgite:/bin/sh gitevivek:x:1002:1002::/home/gitevivek:/bin/sh
Search case‑insensitively:
grep -i -w vivek /etc/passwd
Search for either vivek or raj (case‑insensitive, whole word):
grep -E -i -w 'vivek|raj' /etc/passwd
Anchor a pattern to the start of a line:
grep ^vivek /etc/passwd
Match lines that start with vivek but not vivekgite or vivekg :
grep -w ^vivek /etc/passwd
Match lines ending with foo :
grep 'foo$' FILENAME
Search for empty lines:
grep '^$' FILENAME
2. Matching Specific Characters
Match Vivek or vivek :
grep '[vV]ivek' FILENAME
Or using a fully case‑insensitive character class:
grep '[vV][iI][Vv][Ee][kK]' FILENAME
Match a digit after the name:
grep -w '[vV]ivek[0-9]' FILENAME
Match two‑digit numbers after foo :
grep 'foo[0-9][0-9]' FILENAME
Match any alphabetic character:
grep '[A-Za-z]' FILENAME
Match lines containing either w or n :
grep [wn] FILENAME
POSIX character classes (e.g., [:digit:] , [:upper:] ) can be used inside brackets. Example – match all uppercase letters:
grep '[:upper:]' FILENAME
3. Using Wildcards
The dot . matches any single character. Example – three‑character words that start with b and end with t :
grep '\<b.t\>' FILENAME
Match exactly two characters on a line:
grep '^..$' FILENAME
Match a literal dot in an IP address (escape the dot):
grep '192\.168\.1\.254' /etc/hosts
Extended regex for an IP address:
egrep '[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}' FILENAME
Case‑insensitive match for Linux or Unix :
egrep -i '^(linux|unix)' FILENAME
4. Advanced grep Patterns
Search for a pattern that begins with a hyphen using -e :
grep -e '--test--' FILENAME
OR logic with extended regex:
grep -E 'word1|word2' FILENAME
OR can also be written with escaped pipe:
grep 'word1\|word2' FILENAME
AND logic by piping two greps:
grep 'word1' FILENAME | grep 'word2'
Complex AND using a single expression:
grep 'foo.*bar\|word3.*word4' FILENAME
Quantifiers to test repetitions:
{N} – exactly N occurrences
{N,} – N or more occurrences
{min,max} – between min and max occurrences
Match strings containing two consecutive v characters:
egrep "v{2}" FILENAME
Match col or cool :
egrep 'co{1,2}l' FILENAME
Match at least three c characters:
egrep 'c{3,}' FILENAME
Match a phone number of the form 91-1234567890 (two digits, optional space or hyphen, ten digits):
grep "[[:digit:]]\{2\}[ -]\?[[:digit:]]\{10\}" FILENAME
5. Highlighting and Output Control
Highlight matches:
grep --color regex FILENAME
Show only the matching part of each line:
grep -o regex FILENAME
6. grep vs egrep
egrep is equivalent to grep -E and treats the pattern as an extended regular expression. In basic regular expressions, meta‑characters such as ? , + , { , | , ( , ) lose their special meaning unless escaped.
References: GNU grep manual, info pages, and online regex documentation.
Qunar Tech Salon
Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.