Skip to main content

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.

info

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

tip

💡 Print Specific Lines

sed -n '3p' file.txt  # Print only the 3rd line
tip

🗑 Delete Lines

sed '2d' file.txt  # Delete the 2nd line
tip

🔄 Find and Replace

sed 's/old/new/g' file.txt  # Replace 'old' with 'new' globally
tip

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

tip

📌 Print Specific Columns

awk '{print $1, $3}' file.txt  # Print 1st and 3rd columns
tip

📊 Filter Lines Based on Condition

awk '$3 > 50 {print $0}' file.txt  # Print lines where 3rd column > 50
tip

🔄 Find and Replace

awk '{gsub(/old/, "new"); print}' file.txt  # Replace 'old' with 'new'
tip

🔢 Count Lines Matching a Pattern

awk '/pattern/ {count++} END {print count}' file.txt

⚖ Key Differences Between sed and awk

info
Featuresedawk
Primary UseText substitution & filteringData extraction & processing
Syntax ComplexitySimpleMore powerful & flexible
Supports ArithmeticNoYes
Works Line-by-LineYesYes, but also processes fields

🎯 Conclusion

success
  • ✅ Use sed for simple text replacements, deletions, and insertions.
  • ✅ Use awk for more advanced text processing, such as column-based manipulations and calculations.

🚀 Mastering sed and awk can significantly enhance your Linux text processing skills!