π§Ύ 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?
Feature | awk | sed |
---|---|---|
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 for | Reports, CSVs, filters | Inline 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 :