LUNAROPS · OPERATIONAL UPLINK 100% UPTIME 1,247d POSTS 893 JEFF.MOON@LUNAROPS.DEV UTC --:--:--

Mastering grep for Text Searching

grepregextext-processingcli

grep is one of the most powerful Unix tools. Master it, and you’ll find information faster than any IDE.

Basic Usage

1
2
3
4
5
6
7
8
# Search for pattern in file
grep "error" logfile.txt

# Search in multiple files
grep "error" *.log

# Recursive search
grep -r "TODO" ./src

Essential Flags

Case Insensitivity

1
2
grep -i "error" logfile.txt
# Matches: error, Error, ERROR, eRrOr

Line Numbers

1
2
3
grep -n "function" script.js
# 10:function doSomething() {
# 25:function doAnother() {

Count Matches

1
2
grep -c "error" logfile.txt
# 42

Invert Match

1
2
# Show lines that DON'T match
grep -v "debug" logfile.txt

Show Only Matching Part

1
2
3
grep -o "error.*" logfile.txt
# error: connection refused
# error: timeout

Context Lines

1
2
3
4
5
6
7
8
# 3 lines before match
grep -B 3 "exception" logfile.txt

# 3 lines after match
grep -A 3 "exception" logfile.txt

# 3 lines before and after
grep -C 3 "exception" logfile.txt

Regular Expressions

Basic Patterns

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Any character
grep "err.r" logfile.txt  # error, errar, err0r

# Start of line
grep "^Error" logfile.txt

# End of line
grep "failed$" logfile.txt

# Zero or more
grep "go*gle" file.txt  # ggle, gogle, google, gooogle

Extended Regex (-E)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# One or more
grep -E "go+gle" file.txt  # gogle, google, gooogle

# Optional
grep -E "colou?r" file.txt  # color, colour

# Alternation
grep -E "error|warning|fatal" logfile.txt

# Grouping
grep -E "(ab)+" file.txt  # ab, abab, ababab

Character Classes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Digits
grep "[0-9]" file.txt

# Letters
grep "[a-zA-Z]" file.txt

# Not these characters
grep "[^0-9]" file.txt  # Non-digits

# Word characters
grep -E "\w+" file.txt

Practical Examples

Find IP Addresses

1
grep -E "\b[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\b" access.log

Find Email Addresses

1
grep -E "[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}" file.txt

Find Function Definitions

1
2
3
4
5
# Python
grep -E "^def \w+\(" *.py

# JavaScript
grep -E "(function \w+|const \w+ = .*=>)" *.js

Find TODO Comments

1
grep -rn "TODO\|FIXME\|HACK" ./src

Find Empty Lines

1
grep -c "^$" file.txt

Combining with Other Commands

With find

1
2
3
4
5
# Search specific file types
find . -name "*.py" -exec grep -l "import requests" {} \;

# Or with xargs
find . -name "*.py" | xargs grep "import requests"

With ps

1
2
# Find process
ps aux | grep nginx

With history

1
2
# Search command history
history | grep docker

With pipe

1
2
# Count errors per type
cat logfile.txt | grep "error" | sort | uniq -c | sort -rn

Performance Tips

Use Fixed Strings (-F)

1
2
# Faster when you don't need regex
grep -F "exact string" file.txt

Limit Output (-m)

1
2
# Stop after 10 matches
grep -m 10 "pattern" hugefile.txt

Binary Files

1
2
3
4
5
# Treat binary as text
grep -a "pattern" binaryfile

# Skip binary files
grep -I "pattern" *

ripgrep: A Faster Alternative

If you search code often:

1
2
3
4
5
6
7
# Install
apt install ripgrep  # or brew install ripgrep

# Usage (respects .gitignore by default)
rg "pattern"
rg -i "pattern"  # case insensitive
rg -t py "import"  # only Python files

Cheat Sheet

1
2
3
4
5
6
7
8
9
grep "pattern" file       # Basic search
grep -i "pattern" file    # Case insensitive
grep -r "pattern" dir     # Recursive
grep -n "pattern" file    # Line numbers
grep -c "pattern" file    # Count
grep -v "pattern" file    # Invert
grep -E "pat1|pat2" file  # Extended regex
grep -l "pattern" *.txt   # Files with matches
grep -L "pattern" *.txt   # Files without matches

grep is deceptively simple but incredibly powerful. Invest time in learning regex and you’ll use grep daily.

Comments