What are grep, fgrep, egrep?
1️⃣ grep – General Pattern Search (Basic Regular Expressions)
1)grep command:-(global regular expression print)
- Uses Basic Regular Expressions (BRE)
- Most commonly used command
Syntax
grep "pattern" file_name
Example
grep "unix" notes.txt
grep "m" a.txt
grep "M" a.txt
grep -i "M" a.txt #case insensitive
grep -c "M" a.txt #line count
grep -n "M" a.txt #line number
grep -w "MM" a.txt # word
grep -f a.txt b.txt #mathching of two file
grep -L "unix" * #file names that matches & pattern
🔹 Searches lines containing unix
Using regex
grep "^u" notes.txt
🔹 Lines starting with u
2️⃣ fgrep – Fixed String Search (Fast & Simple)
2)fgrep command:-fixed-character strings in a file
- No regular expressions
- Searches exact words or strings
- Faster for plain text
Syntax
fgrep "pattern" file_name
Example
fgrep "unix|linux" notes.txt
fgrep "mm" a.txt
fgrep -c "m" a.txt #line number
fgrep "m" *.txt
fgrep -l "@a" a.txt #print filename
fgrep -n "@a" a.txt #print line number
fgrep -x "a" a.txt #display only lines matched entirely.
🔹 Searches the exact text unix|linux
(Not OR condition)
✅ Best when:
- You don’t want regex
- You want speed
3️⃣ egrep – Extended Regular Expressions
- Uses Extended Regular Expressions (ERE)
- Supports
|,+,?,()
Syntax
egrep "pattern" file_name
Example
egrep "unix|linux" notes.txt
egrep "mital" * #globally search
egrep "m" a.txt
egrep -c "mm" a.txt #linenumber mathched
egrep -l "mm" a.txt # print filename
egrep -n "mm" a.txt # print linenumber
egrep -r "m*" m.txt
egrep -r "m*" . #recursively searching whole root
🔹 Matches unix OR linux
egrep "(cat|dog)s?" animals.txt
🔹 Matches cat, cats, dog, dogs
🔁 Key Differences (Very Important for Exams)
| Feature | grep | fgrep | egrep |
|---|---|---|---|
| Pattern type | Basic Regex | Fixed string | Extended Regex |
| Supports ` | ` (OR) | ❌ | ❌ |
Supports +, ?, () | ❌ | ❌ | ✅ |
| Fastest | ❌ | ✅ | ❌ |
| Most flexible | ❌ | ❌ | ✅ |
⚠️ Important Note (Modern UNIX/Linux)
Today:
fgrep=grep -Fegrep=grep -E
Examples
grep -F "unix|linux" file.txt
grep -E "unix|linux" file.txt
📝 Easy Memory Trick
- grep → basic search
- fgrep → fixed text (no regex)
- egrep → extra power (OR, +, ?)