🛠️ Bash Power Tools: grep
, awk
, sed
, cut
, and tr
Explained with Comparison.
When working in Bash, you’ll come across several powerful text-processing tools. Each of them — grep
, awk
, sed
, cut
, and tr
— has a unique role. Here's a deep dive into what each one does, when to use them, and how they compare.
📊 Text Processing Tools Comparison Table
Tool | Primary Use | Works On | Supports Patterns | Field/Column Support | Modifies Text | Best Use Case |
---|---|---|---|---|---|---|
grep |
Search/filter lines with a pattern | Full lines | ✅ Regex | ❌ No | ❌ No | Search for matching lines (logs, files) |
awk |
Field-wise processing + logic | Line-by-line | ✅ Regex + conditions | ✅ Yes | ✅ Yes | Column-wise reporting, filtering, summaries |
sed |
Find & replace, stream editing | Line-by-line | ✅ Regex | ✅ Limited (via regex) | ✅ Yes | Replace/edit text in streams/files |
cut |
Extract specific fields/characters | Line-by-line | ❌ No | ✅ Yes (-d , -f ) |
❌ No | Quickly extract columns by delimiter |
tr |
Translate/delete characters | Char-by-char | ❌ No | ❌ No | ✅ Yes (chars) | Replace or remove specific characters |
💡 When to Use What
Scenario | Use Tool | Why? |
---|---|---|
Find lines containing "ERROR" in logs |
grep |
Simple pattern match |
Replace "localhost" with "127.0.0.1"
|
sed |
Fast line/text replacement |
Convert lowercase to uppercase in a file | tr |
Char-level transformation |
Extract usernames from /etc/passwd
|
cut |
Fixed delimiter (:) and fields |
Print names and emails from CSV, filter age > 25 | awk |
Column-based logic and filtering |
Delete 2nd line from a file | sed '2d' |
Line manipulation |
Remove digits from a string | tr -d '0-9' |
Clean unwanted characters |
🧠 Quick Summary
Tool | Simple | Powerful | Script-Friendly | Supports Logic |
---|---|---|---|---|
grep |
✅ | ⚠️ Only for matching | ✅ | ❌ |
cut |
✅ | ⚠️ Fixed format only | ✅ | ❌ |
tr |
✅ | ⚠️ Char-level only | ✅ | ❌ |
sed |
✅ | ✅ | ✅ | ⚠️ Basic logic |
awk |
⚠️ | ✅✅ | ✅ | ✅✅ |
✂️ cut (Extract fields)
echo "Alice,25,Developer,NY" | cut -d ',' -f 1
echo "Bob,30,Manager,LA" | cut -d ',' -f 2,3
🧠 awk (Fields + Logic)
echo "Eve,28,Developer,TX" | awk -F ',' '{ print $1, $3 }'
echo "Daniel,35,CEO,CA" | awk -F ',' '$2 > 30 { print $1 " is senior" }'
🔤 tr (Character transformations)
echo "developer" | tr 'a-z' 'A-Z'
echo "Remove123Digits" | tr -d '0-9'
echo "one:two:three" | tr ':' '-'
🧹 sed (Stream edit)
echo "Welcome dev!" | sed 's/dev/developer/'
echo "Line to delete" | sed '1d'
echo "TX" | sed 's/^/[Location] /'
🔍 grep – The Pattern Hunter
grep is used to search for lines that match a pattern. It's perfect for filtering logs, outputs, or data streams.
✅ Basic Examples with echo:
echo "Alice,25,Developer,NY" | grep "Developer"
# Output: Alice,25,Developer,NY
echo "Bob,30,Manager,LA" | grep "Developer"
# Output: (nothing, because it doesn't match)
echo "Charlie,22,intern,TX" | grep -i "Intern"
# Output: Charlie,22,intern,TX
# -i makes it case-insensitive
echo "Daniel,35,CEO,CA" | grep -v "Developer"
# Output: Daniel,35,CEO,CA
# -v inverts the match (shows non-matching lines)
🧠 Explanation:
grep "pattern" – Matches lines containing pattern.
-i – Case-insensitive match.
-v – Show only lines that don’t match the pattern.
-E – Use extended regular expressions (for advanced patterns).
-o – Show only the matching part of the line.
📌 Conclusion
Each of these Bash superheroes has a specialty. Combine them like Unix Avengers and you can process any kind of text or log stream like a pro!