Text Processing with grep, sed, awk
Search, filter, and transform text files using Unix's most powerful tools.
What You'll Learn
- grep — search for patterns in files
- sed — find and replace text in streams
- awk — extract and process columns
- Combining tools for real-world tasks
grep — Global Regular Expression Print
grep is the Swiss Army knife of text searching. It scans files line by line and prints any line matching your pattern.
🔍 Real-World Analogy: Imagine highlighting every sentence in a book that contains a specific word. That's exactly what grep does — but across thousands of files in milliseconds.
$ grep "error" log.txt # Find lines with "error" $ grep -i "error" log.txt # Case-insensitive $ grep -n "error" log.txt # Show line numbers $ grep -c "error" log.txt # Count matches $ grep -v "info" log.txt # Exclude lines with "info" $ grep -r "TODO" src/ # Search recursively in directory $ grep -l "import" src/*.tsx # List files containing match
⚠️ Common Mistake
Forgetting -r when searching directories. grep "text" folder/ won't work — you need grep -r "text" folder/.
grep
Search and filter text in log files.
// grep — Search Text in Files — simulated
console.log("=== grep Basics ===");
console.log('$ grep "error" server.log');
console.log(" → Find all lines containing 'error'");
console.log();
// Simulate grep
const logFile = [
"[INFO] Server started on port 3000",
"[ERROR] Database connection failed",
"[INFO] Request GET /api/users",
"[WARN] Slow query detected (2.3s)",
"[ERROR] Timeout on external API call",
"[INFO] Request POST /api/login",
"[ERROR] Invalid credentials for use
...sed and awk
sed — Stream Editor
sed transforms text line by line. Its most common use is find-and-replace:
$ sed 's/old/new/' file.txt # Replace first match per line $ sed 's/old/new/g' file.txt # Replace ALL matches (global) $ sed -i 's/old/new/g' file.txt # Edit file in-place $ sed '3d' file.txt # Delete line 3 $ sed '/pattern/d' file.txt # Delete lines matching pattern
awk — Column Processor
awk treats each line as a record with fields separated by whitespace:
$ awk '{print $1}' file.txt # Print first column
$ awk '{print $1, $3}' file.txt # Print columns 1 and 3
$ awk -F',' '{print $2}' data.csv # Use comma as delimiter
$ awk '$3 > 100 {print $1}' data # Conditional: column 3 > 100
$ awk '{sum += $3} END {print sum}' # Sum all values in column 3💡 Pro Tip
Combine all three: grep "ERROR" log.txt | sed 's/\[ERROR\]/⛔/' | awk '{print $1, $2}'. This filters errors, reformats them, and extracts timestamps.
sed & awk
Transform text and extract data from columns.
// sed & awk — Transform Text — simulated
console.log("=== sed — Stream Editor ===");
console.log("Find and replace text in files:");
console.log();
console.log("$ sed 's/old/new/' file.txt (first occurrence per line)");
console.log("$ sed 's/old/new/g' file.txt (ALL occurrences — global)");
console.log("$ sed -i 's/old/new/g' file.txt (edit file IN PLACE)");
console.log();
// Simulate sed
const config = [
"host=localhost",
"port=3000",
"debug=true",
"database=myapp_dev",
];
cons
...📋 Quick Reference
| Command | Description |
|---|---|
| grep -ri "text" dir/ | Recursive case-insensitive search |
| grep -v "exclude" | Invert match (exclude lines) |
| sed 's/old/new/g' | Global find and replace |
| sed -i | Edit file in-place |
| awk '{print $1}' | Print first column |
| awk -F',' | Set field delimiter |
🎉 Lesson Complete!
You can now search, filter, and transform text like a Unix wizard! Next: pipes and redirection — chaining commands together for maximum power.
Sign up for free to track which lessons you've completed and get learning reminders.