Basics of sed and awk
🔍 Introduction
sed (Stream Editor) and awk (Text Processing Tool) are powerful command-line utilities for text manipulation in Linux. They are widely used for pattern searching, text replacement, and data extraction.
Why Learn sed and awk?
Mastering these tools enables efficient text manipulation, automation of repetitive tasks, and advanced data processing in Linux environments.
🛠 sed - Stream Editor
sed is primarily used for modifying or filtering text in a stream or file.
🔹 Basic Syntax
sed [OPTIONS] 'COMMAND' file
✨ Common sed Commands
💡 Print Specific Lines
sed -n '3p' file.txt # Print only the 3rd line
🗑 Delete Lines
sed '2d' file.txt # Delete the 2nd line
🔄 Find and Replace
sed 's/old/new/g' file.txt # Replace 'old' with 'new' globally
➕ Insert and Append Text
sed '3i\New line before line 3' file.txt # Insert before line 3
sed '3a\New line after line 3' file.txt # Append after line 3
📊 awk - Pattern Scanning & Processing
awk is used for pattern-based scanning, data extraction, and text transformation.
🔹 Basic Syntax
awk 'PATTERN { ACTION }' file
✨ Common awk Operations
📌 Print Specific Columns
awk '{print $1, $3}' file.txt # Print 1st and 3rd columns
📊 Filter Lines Based on Condition
awk '$3 > 50 {print $0}' file.txt # Print lines where 3rd column > 50
🔄 Find and Replace
awk '{gsub(/old/, "new"); print}' file.txt # Replace 'old' with 'new'
🔢 Count Lines Matching a Pattern
awk '/pattern/ {count++} END {print count}' file.txt
⚖ Key Differences Between sed and awk
| Feature | sed | awk |
|---|---|---|
| Primary Use | Text substitution & filtering | Data extraction & processing |
| Syntax Complexity | Simple | More powerful & flexible |
| Supports Arithmetic | No | Yes |
| Works Line-by-Line | Yes | Yes, but also processes fields |
🎯 Conclusion
- ✅ Use
sedfor simple text replacements, deletions, and insertions. - ✅ Use
awkfor more advanced text processing, such as column-based manipulations and calculations.
🚀 Mastering sed and awk can significantly enhance your Linux text processing skills!