Skip to content
Log In Sign Up info@muvcloud.com

The awk Commands You Will Use Most

awk '{print $1}' /var/log/nginx/access.log
awk -F: '{print $1}' /etc/passwd
awk '$9 == 404' /var/log/nginx/access.log
awk '{sum += $10} END {print sum}' /var/log/nginx/access.log

An awk program is written in single quotes so the shell leaves the $ signs alone. $1 is the first field of the line, $2 the second, and $0 the whole line. The examples print the client address of each request, list the user names on the system, show requests with status 404, and add up the bytes sent.

How awk Splits a Line

By default, awk splits on runs of spaces and tabs, so several spaces between columns count as one separator. For files that use another character, set it with -F:

awk -F: '{print $1, $7}' /etc/passwd
awk -F, '{print $2}' data.csv

A comma between fields in print outputs them with a space between. Without the comma, they are joined with nothing between. Simple -F, does not understand quoted CSV fields that contain commas.

Built-in Variables

VariableMeaning
NRLine number
NFNumber of fields
$NFThe last field
FSInput separator
OFSOutput separator
awk 'NR >= 10 && NR <= 20' file.txt
awk '{print NR": "$0}' file.txt
awk 'NF == 0' file.txt

These print lines 10 to 20, number every line, and show empty lines.

Filter Lines with Conditions

A condition before the braces decides which lines the program runs on. A condition alone prints the matching lines:

awk -F: '$3 >= 1000 && $3 < 65534 {print $1}' /etc/passwd
awk '/error/' app.log
awk '$9 >= 500 {print $7}' /var/log/nginx/access.log

The first lists regular user accounts, which have IDs from 1000 on Debian, Ubuntu, AlmaLinux, and Rocky Linux; the upper limit leaves out the nobody account, which has ID 65534. The second works like grep. The third prints the requested path of every server error.

Sum and Count

awk '{sum += $10} END {print sum / 1024 / 1024 " MB"}' /var/log/nginx/access.log
awk '{n++} END {print n}' file.txt

Variables start at zero, so no setup is needed. The END block runs once after the last line; a BEGIN block runs once before the first. To pass a value from the shell, use -v: awk -v min=500 '$9 >= min' access.log.

Parse an Access Log

In the default combined format of Nginx and Apache, a line split on spaces gives these fields: $1 client address, $4 date and time, $6 method with a leading quote, $7 path, $9 status, and $10 bytes sent. Two useful reports:

awk '{print $9}' access.log | sort | uniq -c | sort -rn
awk '{b[$1] += $10} END {for (ip in b) print b[ip], ip}' access.log | sort -rn | head

The first counts requests per status code. The second adds up bytes per client and shows the ten heaviest. If you changed the log format, count the fields again. For more on ranking, see sort and uniq; to watch the log live, see tail -f.

Which awk You Have

On Debian and Ubuntu, awk is mawk unless the gawk package is installed, which then takes over the awk name. AlmaLinux and Rocky Linux use GNU awk (gawk). readlink -f "$(command -v awk)" shows which one you have. Every example on this page works with both. Some gawk features, such as gensub() or in-place editing, are missing from mawk; install the gawk package if you need them. For counting lines without fields, wc is simpler.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations