π οΈ 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!
Top comments (0)