Prepared by: Anwer Sadath Abdhul Muttaliff
awk
is a powerful text-processing tool for Unix/Linux systems. It reads input line by line, splits lines into fields, and allows you to manipulate and extract data efficiently. This guide covers basic to intermediate awk
usage, with practical examples for system administrators and developers.
awk 'pattern {action}' file
Print every line of the file:
awk '{print}' file.txt
Print the first field (column) of each line:
awk '{print $1}' file.txt
Fields are separated by spaces/tabs by default.
Use a custom delimiter (e.g., commas):
awk -F ',' '{print $1}' file.csv
Print lines containing "pattern":
awk '/pattern/ {print}' file.txt
awk 'END {print NR}' file.txt
awk '{print toupper($0)}' file.txt
Print fields based on conditions:
awk '{if ($3 > 50) print $1, $2}' file.txt
String manipulation:
awk '{print substr($1, 1, 3)}' file.txt
Mathematical operations:
awk '{sum += $2} END {print sum}' file.txt
Count lines in a file:
awk '{count++} END {print "Total lines:", count}' file.txt
Process multiple files:
awk '{print FILENAME, $0}' file1.txt file2.txt
Extract error messages:
awk '/error/ {print}' /var/log/syslog
Count occurrences of a keyword:
awk '/error/ {count++} END {print count}' /var/log/syslog
Summarize disk usage:
df -h | awk 'NR>1 {print $1, $5}'
Filter high CPU processes:
ps aux | awk '$3 > 80 {print $1, $2, $3, $11}'
List usernames and shells:
awk -F ':' '{print $1, $7}' /etc/passwd
Generate CSV-like output:
ls -l | awk 'BEGIN {print "File, Size, Owner"} {print $9, $5, $3}'
awk
is an indispensable tool for text processing and automation in Unix/Linux systems. Whether you're analyzing logs, managing user accounts, or generating reports, awk
offers a powerful and efficient solution. By mastering these techniques, you can streamline your workflow and enhance your productivity as a system administrator or developer.