awk Command — The Ultimate Linux Text Processing Language¶
awkis one of the most powerful text-processing tools available on Linux. Unlike commands such asgrep,cut, orsed,awkis a complete programming language designed for processing structured text, generating reports, performing calculations, filtering data, and automating repetitive tasks. It is widely used in Linux administration, DevOps, Cloud Engineering, Data Processing, and Security.
Learning Path¶
Course Progress
What You'll Learn¶
After completing this lesson, you'll be able to:
- Understand the
awkprogramming model - Work with fields and records
- Filter and print data
- Perform calculations
- Use variables
- Apply conditional statements
- Generate reports
- Process CSV files
- Use built-in variables
- Write production-ready automation
Prerequisites¶
Complete:
- Module 1
- Module 2
- Module 3 Lessons 1–16
Why Learn awk?¶
Suppose you have:
Questions:
- Show employees earning more than 80,000.
- Calculate total salary.
- Find average salary.
- Display only names.
- Sort departments.
- Generate reports.
All of this can be done using awk.
What is awk?¶
awk is a pattern-scanning and text-processing language.
It works line by line.
Each line is automatically divided into fields.
Default field separator:
Syntax:
Sample File¶
Create:
Contents:
Print Entire File¶
Equivalent to:
Print First Column¶
Output:
Print Multiple Columns¶
Output:
Built-in Variables¶
| Variable | Description |
|---|---|
$1 | First field |
$2 | Second field |
$NF | Last field |
NR | Current record number |
NF | Number of fields |
FS | Input field separator |
OFS | Output field separator |
Print Last Column¶
Output:
Print Record Number¶
Output:
Conditional Statements¶
Employees earning more than 80,000.
Output:
Exact Match¶
Multiple Conditions¶
BEGIN Block¶
Output:
END Block¶
BEGIN + END¶
Calculations¶
Total salary.
Output:
Average salary.
Maximum salary.
Count Records¶
CSV Files¶
Create:
Specify delimiter.
Output Separator¶
Formatted Reports¶
Output:
Using Variables¶
Using Regular Expressions¶
Contains "Eng".
Does not contain HR.
Common awk Options¶
| Option | Description |
|---|---|
-F | Input delimiter |
-v | Pass variable |
-f | Run AWK program from file |
Real Production Examples¶
Display usernames.
Display login shells.
Disk usage.
Docker.
Kubernetes.
Systemd.
Kubernetes Example¶
Display only Running Pods.
Docker Example¶
Display image names.
Terraform Example¶
Extract resource names.
Production Perspective¶
awk is heavily used for:
- CSV processing
- Report generation
- Monitoring
- Kubernetes administration
- Log analysis
- CI/CD
- Security auditing
- Automation
Hands-on Lab¶
Task 1¶
Display names.
Task 2¶
Display salary.
Task 3¶
Display Engineering.
Task 4¶
Calculate total salary.
Task 5¶
Average salary.
Task 6¶
Number rows.
Task 7¶
CSV processing.
Task 8¶
Pretty report.
Production Troubleshooting Scenario¶
Scenario
A Kubernetes administrator needs to generate a report showing only Running Pods with their node names.
Tasks:
- Filter Running Pods.
- Display Pod Name.
- Display Node.
- Save the report.
Best Practices¶
- Use
-Ffor CSV and colon-separated files. - Use
printffor professional reports. - Prefer
awkover multiplecutcommands when performing calculations. - Keep complex scripts in separate
.awkfiles. - Comment large AWK programs for maintainability.
Performance Tip¶
awk processes input as a stream, making it efficient for large files. It often replaces multiple chained commands (cut, grep, sort) with a single, readable program, reducing process creation overhead.
Common Mistakes¶
❌ Forgetting field numbering starts at 1.
✅ Remember to field numbering starts at 1.
❌ Using cut when calculations are required.
✅ Use awk instead.
❌ Not specifying the correct delimiter.
✅ Use:
Interview Questions¶
Beginner¶
- What is
awk? - What does
$1represent? - What is
NR? - What is
NF?
Intermediate¶
- Explain
BEGINandEND. - Difference between
printandprintf. - What does
-Fdo? - How do you calculate averages?
Architect Level¶
- Why is
awkconsidered a programming language? - How would you generate infrastructure reports using
awk? - When would you choose
awkinstead ofsedorcut?
Summary¶
In this lesson, you learned:
- Field-based processing
- Conditional filtering
- Variables and built-in variables
- Calculations
- CSV processing
- Report generation
- Production automation
awk is one of the most powerful tools in Linux. Mastering it enables you to process structured text, generate reports, automate administrative tasks, and build efficient DevOps workflows.
Key Takeaways¶
awkis a complete text-processing language.- Fields are referenced using
$1,$2, ...,$NF. - Use
-Fto specify field separators. BEGINandENDare ideal for headers, summaries, and totals.printfcreates professional, aligned reports.awkis a core skill for Linux administrators, DevOps engineers, SREs, and Cloud Architects.
What's Next?¶
Regular Expressions (Regex) — Pattern Matching in Linux
In the next lesson, you'll learn pattern matching for grep, sed, and awk — anchors, character classes, quantifiers, and production-ready regex techniques.