Linux AWK command Tutorial for Beginners | Learn AWK with Examples, learn Step-by-step AWK tutorial for Linux beginners. Learn AWK command syntax, patterns, and examples to process text and data efficiently in Linux.
In Linux, text processing is one of the most powerful tasks that system administrators and developers perform on a daily basis. Among the many command-line tools available, AWK stands out as one of the most versatile and powerful utilities for pattern scanning and processing. This tutorial will guide you through the basics of the AWK command in Linux, its syntax, and practical examples to help you master it.
What is Linux AWK command
The awk command in Linux is not an acronym with a “full form” in the traditional sense of standing for a specific phrase or set of words describing its function. Instead, awk is named after the initials of its three original developers:
- A – lfred Aho
- W – einberger (Peter Weinberger)
- K – ernighan (Brian Kernighan)
Basic syntex of Linux AWK command
awk 'pattern { action }' filename
- Pattern – Defines what to search for in each line.
- Action – Defines what to do if the pattern matches.
- Filename – The input file to process.
Note: If no pattern is given, AWK performs the action on all lines. If no action is given, AWK prints the matching line by default.
awk command list List files with sizes
# List files with sizes ls -l | awk '{print $9, $5}'
Learn AWK with Examples
1. Print All Lines
awk '{ print }' file.txt
2. Print Specific Column
awk '{ print $1, $3 }' file.txt
3. Print Line Numbers
awk '{ print NR, $0 }' file.txt
4. Conditional Pattern Matching
awk '/error/ { print }' log.txt
5. Print Lines Based on Condition
awk '$3 > 50 { print $1, $3 }' marks.txt
Real-World Examples of AWK
Example 1: Summing a Column
awk '{ sum += $2 } END { print "Total:", sum }' sales.txt
Example 2: Average Calculation
awk '{ total += $3; count++ } END { print "Average:", total/count }' marks.txt
Example 3: Replace Field Separator
awk 'BEGIN { FS=":"; OFS=" - " } { print $1, $2 }' /etc/passwd
AWK Built-in Variables
Variable | Meaning |
---|---|
$0 | Whole line |
$1, $2… | Specific column/field |
NR | Number of records (lines) |
NF | Number of fields in the current line |
FS | Field separator (default is space) |
OFS | Output field separator |
RS | Record separator |
ORS | Output record separator |
Why Use AWK Linux commans?
- Quick and efficient for log analysis.
- Ideal for column-based data manipulation.
- Works well in shell scripting.
Lightweight alternative to Python or Perl for simple text tasks.
Conclusion
The AWK command in Linux is a must-know tool for anyone working with text, data files, or system administration. With its pattern scanning and processing power, it allows you to filter, format, and analyze data efficiently. By practicing the examples above, you’ll be well on your way to mastering AWK for real-world use cases.
If you have and query or suggestion or need assistance then please contact me, I will reply to fix your problem, if you like our content then you can subscribe to our Youtube channel. If you want to hire me then reach us at our Fiverr.