DEV Community

DevOps Man
DevOps Man

Posted on

πŸ¦Έβ€β™‚οΈπŸ› οΈ The Avengers of Bash: grep, awk, sed, cut & tr Assembled! βš‘πŸ§πŸ’»

πŸ› οΈ 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
Enter fullscreen mode Exit fullscreen mode

🧠 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" }'
Enter fullscreen mode Exit fullscreen mode

πŸ”€ tr (Character transformations)

echo "developer" | tr 'a-z' 'A-Z'
echo "Remove123Digits" | tr -d '0-9'
echo "one:two:three" | tr ':' '-'
Enter fullscreen mode Exit fullscreen mode

🧹 sed (Stream edit)

echo "Welcome dev!" | sed 's/dev/developer/'
echo "Line to delete" | sed '1d'
echo "TX" | sed 's/^/[Location] /'
Enter fullscreen mode Exit fullscreen mode

πŸ” 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
Enter fullscreen mode Exit fullscreen mode

echo "Bob,30,Manager,LA" | grep "Developer"
# Output: (nothing, because it doesn't match)
Enter fullscreen mode Exit fullscreen mode

echo "Charlie,22,intern,TX" | grep -i "Intern"
# Output: Charlie,22,intern,TX
# -i makes it case-insensitive
Enter fullscreen mode Exit fullscreen mode

echo "Daniel,35,CEO,CA" | grep -v "Developer"
# Output: Daniel,35,CEO,CA
# -v inverts the match (shows non-matching lines)
Enter fullscreen mode Exit fullscreen mode

🧠 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!

Top comments (0)