Linux awk Command: Work with Columns
awk splits each line into columns, called fields, so you can pick out the parts you want, filter rows and add up numbers. It is ideal for log files, CSV files and command output.
Printing columns
awk numbers the columns $1, $2, $3 and so on. $0 is the whole line. By default columns are separated by spaces.
awk '{print $1}' access.log
Prints the first column of each line. In a web server log this is the visitor's IP address.
awk '{print $1, $7}' access.log
Prints the IP address and the page that was requested.
awk -F, '{print $1}' names.csv
Uses a comma as the separator (-F,) and prints the first column of a CSV file.
awk -F: '{print $1, $7}' /etc/passwd
Lists each user account and its login shell.
Filtering rows
awk '$9 == 404' access.logShows only requests that returned a 404 Not Found error.
awk 'NR > 1' names.csvSkips the first line. NR is the line number.
awk 'length > 80' notes.txtShows lines longer than 80 characters.
Adding things up
awk -F, 'NR > 1 {total += $3} END {print total}' names.csv
Adds up the third column, skipping the heading row, and prints the total at the end.
awk -F, 'NR > 1 {sum[$2] += $3} END {for (c in sum) print c, sum[c]}' names.csv
Adds up the amounts for each city.
awk 'END {print NR}' access.log
Counts the lines in a file.
Using awk with other commands
df -h | awk 'NR > 1 && $5+0 > 80 {print $6, $5}'
Lists any disks that are more than 80% full.
Quick reference
| Option | Meaning |
|---|---|
$1, $2 … | Column 1, column 2 … |
$0 | The whole line |
$NF | The last column |
NR | Current line number |
-F, | Use a comma as the separator |
'$3 > 100' | Only lines where column 3 is over 100 |
END {…} | Run once after the last line |