AWK Commands Cheatsheet — Essential Reference
Awk is a powerful text processing tool that allows you to manipulate data and generate reports based on patterns and actions. It's particularly useful for extracting and transforming data from structured text files, often line by line.
Master AWK with Top Reference Books
Level up your DevOps skills. Find the best guide books and cheatsheet references for your engineering stack.
Commands
| Command | Description | Example |
|---|---|---|
awk '{ print }' file.txt |
Prints every line of the file. This is the default action if no pattern is specified. | awk '{ print }' data.log |
awk '/pattern/ { action }' file.txt |
Executes 'action' for lines matching 'pattern'. | awk '/error/ { print $0 }' system.log |
awk '{ print $1, $3 }' file.txt |
Prints the first and third fields of each line. Fields are typically space-separated by default. | awk '{ print $1, $3 }' users.csv |
awk -F',' '{ print $2 }' file.csv |
Sets the field separator to a comma and prints the second field. | awk -F':' '{ print $1 }' /etc/passwd |
awk 'BEGIN { print "Header" } { print } END { print "Footer" }' file.txt |
Executes actions before processing any lines (BEGIN), for each line, and after all lines are processed (END). | awk 'BEGIN { print "--- Start Report ---" } { print NR, $0 } END { print "--- End Report ---" }' sales.txt |
awk '$3 > 100 { print $1, $3 }' data.txt |
Prints the first and third fields for lines where the third field's value is greater than 100. | awk '$2 == "active" { print $1 }' users.txt |
awk '{ sum += $1 } END { print "Total: ", sum }' numbers.txt |
Calculates the sum of the first field for all lines and prints the total at the end. | awk '{ count++ } END { print "Line count: ", count }' document.txt |
awk '{ if (length($0) > 80) print "Long line: ", $0 }' text.txt |
Uses an 'if' statement to perform actions based on conditions, here printing lines longer than 80 characters. | awk '{ if ($1 ~ /^[0-9]+$/) print $1 }' mixed_data.txt |
Pro Tips
- Use `NR` for the current record number and `NF` for the number of fields in the current record.
- Remember that `awk` variables are typeless; they are treated as numbers or strings as needed.
- For complex scripts, store your `awk` code in a separate file and execute with `awk -f script.awk file.txt`.
Frequently Asked Questions
What is awk used for?
Awk is primarily used for text processing, data extraction, and report generation. It excels at tasks like filtering log files, reformatting data, and performing calculations on tabular data.
How do I install awk?
Awk (or a compatible version like `gawk`) is typically pre-installed on most Unix-like operating systems (Linux, macOS). You can check its presence by typing `awk --version` in your terminal. If not found, you can usually install it via your system's package manager (e.g., `sudo apt-get install gawk` on Debian/Ubuntu, `brew install gawk` on macOS).