πŸ” Linux/Unix: Searching, Text Processing & Regex
Estimated reading: 3 minutes 31 views

🧾 Linux/Unix: Text Processing – awk, sed Explained with Output & Real-World Examples

🧲 Introduction – Why Learn Text Processing with awk and sed?

Linux/Unix provides incredibly powerful tools for processing text directly from the terminal. Whether you’re analyzing logs, formatting reports, or editing config files, awk and sed let you filter, transform, and automate with precision.

🎯 In this guide, you’ll learn:

  • βœ… How to use awk for column-based operations and calculations
  • βœ… How sed can perform inline substitutions and text manipulations
  • βœ… Real-world examples with command output
  • βœ… Best practices and practical use cases for log parsing and data cleaning

πŸ“Š awk – Pattern Scanning and Field Processing Tool

βœ… Syntax:

awk 'pattern { action }' filename

πŸ“ Sample File: students.txt

Alice 85
Bob 92
Charlie 78

πŸ”Ή Example 1: Print First Column (Names)

awk '{print $1}' students.txt

πŸ“€ Output:

Alice
Bob
Charlie

πŸ”Ή Example 2: Print Name and Score

awk '{print "Name:", $1, "- Score:", $2}' students.txt

πŸ“€ Output:

Name: Alice - Score: 85
Name: Bob - Score: 92
Name: Charlie - Score: 78

πŸ”Ή Example 3: Print Students with Score > 80

awk '$2 > 80 {print $1, $2}' students.txt

πŸ“€ Output:

Alice 85
Bob 92

πŸ”Ή Example 4: Calculate Total Score

awk '{sum += $2} END {print "Total:", sum}' students.txt

πŸ“€ Output:

Total: 255

πŸ”Ή Example 5: Using Field Separator (CSV Example)

awk -F ',' '{print $2}' data.csv

πŸ“€ Output: (Assuming data.csv has id,name,city)

name
John
Sarah

πŸ› οΈ sed – Stream Editor for Inline Text Manipulation

βœ… Syntax:

sed [options] 'command' filename

πŸ“ Sample File: logs.txt

Error: Disk full
Warning: CPU high
Error: Memory low

πŸ”Ή Example 1: Replace First “Error” with “ALERT”

sed 's/Error/ALERT/' logs.txt

πŸ“€ Output:

ALERT: Disk full
Warning: CPU high
ALERT: Memory low

πŸ”Ή Example 2: Replace All Occurrences Globally

sed 's/Error/ALERT/g' logs.txt

πŸ“€ Same Output as above (all “Error” replaced with “ALERT”)


πŸ”Ή Example 3: Delete Blank Lines

πŸ“ File: sample.txt

Hello

World
sed '/^$/d' sample.txt

πŸ“€ Output:

Hello
World

πŸ”Ή Example 4: Delete Line Number 2

sed '2d' sample.txt

πŸ“€ Output:

Hello


πŸ”Ή Example 5: Edit File In-Place (Replace http with https)

sed -i 's/http/https/g' urls.txt

βœ… The file urls.txt is now updated with all http links changed to https.


πŸ”„ Combined Use Case: awk + sed Workflow

πŸ”§ Task: Extract usernames from a file and convert to uppercase

πŸ“ File: users.txt

alice
bob
charlie
awk '{print $1}' users.txt | sed 's/.*/\U&/'

πŸ“€ Output:

ALICE
BOB
CHARLIE

βœ… awk extracts names, sed transforms to uppercase.


βš–οΈ awk vs sed – When to Use?

Featureawksed
Works with fields?βœ… Yes❌ No
Works with patterns?βœ… Yesβœ… Yes
Supports math?βœ… Yes (sum, avg, count)❌ No
In-place editing?❌ No (use redirection)βœ… Yes (-i flag)
Best forReports, CSVs, filtersInline substitution, deletions

πŸ“Œ Summary – Recap & Next Steps

Mastering awk and sed gives you a significant advantage in processing structured or unstructured data from logs, config files, reports, and scripts.

πŸ” Key Takeaways:

  • 🟩 Use awk for column operations, calculations, and report formatting
  • 🟨 Use sed for editing streams, substitution, and line manipulation
  • πŸ› οΈ Combine both for advanced automation and log parsing workflows

❓ FAQs

❓ How do I change column separators in awk?
βœ… Use -F to define a new delimiter:

awk -F ':' '{print $1}' /etc/passwd

❓ Can sed make permanent changes to a file?
βœ… Yes, use -i:

sed -i 's/foo/bar/g' file.txt

❓ How do I sum a column using awk?
βœ… Use:

awk '{total += $2} END {print total}' numbers.txt

❓ Can awk and sed be used in shell scripts?
βœ… Absolutelyβ€”they are widely used in bash scripts for parsing, editing, and output formatting.

❓ What’s the difference between awk {print} and sed s/?
βœ… awk processes fields/columns, sed replaces or deletes text via pattern matching.


Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

πŸ”΅ Linux/Unix: Text Processing (awk, sed)

Or Copy Link

CONTENTS
Scroll to Top