awk
awk is a text-processing language that splits each input line into fields and runs pattern-action rules on them. It is used to extract columns, filter rows, and total numbers from logs, CSV files, and command output.
Understanding awk
What Is awk and What Is It Used For?
awk reads input one line at a time, splits the line into numbered fields ($1, $2, $3 and so on), and runs the rules you write against those fields. Where grep only finds lines, awk can also pick columns, do arithmetic, and keep running totals. It is the standard tool for turning raw log lines or ps output into a small, readable report.
# Print only the PID and command columns from ps outputps aux | awk '{print $2, $11}'How awk Splits a Line Into Fields
By default awk splits on runs of whitespace. $0 is the whole line, $NF is the last field, NF is the field count, and NR is the current line number.
Input line: rahul 1234 45.3 2.1 node index.js $1=rahul $2=1234 $3=45.3 $4=2.1 $5=node $6=index.js$0=entire line NF=6 $NF=index.js NR=line numberA program is a list of pattern { action } pairs. If the pattern is omitted, the action runs on every line. If the action is omitted, matching lines are printed. BEGIN runs once before input, END runs once after it.
Practical awk Commands
# Custom delimiter: username and UID from /etc/passwdawk -F: '{print $1, $3}' /etc/passwd # Filter on a condition: processes using more than 10% CPUps aux | awk '$3 > 10 {print $2, $11}' # Filesystems more than 80% full (skip the header, force numeric with +0)df -h | awk 'NR>1 && $5+0 > 80 {print $6, $5}' # Pattern match with line numbersawk '/ERROR/ {print NR": "$0}' /var/log/app.log # Sum a columnawk '{sum += $2} END {print "Total:", sum}' numbers.txt # Count errors per service from lines like: 2026-01-15 10:23:45 payment-api ERROR timeoutawk '/ERROR/ {errors[$3]++} END {for (svc in errors) print svc, errors[svc]}' /var/log/app.log # Formatted outputdf -h | awk 'NR>1 {printf "%-20s %5s\n", $6, $5}' # Lines 10 to 20 onlyawk 'NR>=10 && NR<=20' /var/log/app.log # Remove duplicate lines, keep the header rowawk 'NR==1{print; next} !seen[$0]++' report.txtTroubleshooting awk
| Symptom | Command | What to Check |
|---|---|---|
| Wrong field printed | echo 'a b c' | awk '{print $2}' |
Test splitting on simple input |
| Numeric compare misbehaves | awk '$3+0 > 80' |
Force numeric with +0 |
| CSV columns misaligned | awk -F, '{print $2}' |
Quoted fields containing commas |
| Multi-character separator | awk -F '::' '{print $1}' |
-F accepts a regex |
TipAlways pass a header-skip such as
NR>1when processingdforpsoutput, otherwise the header line gets compared and printed like data.
Common MistakeUsing
-F,on real CSV files. Fields like"Smith, John"get split at the embedded comma. For quoted CSV use gawk'sFPATor a real CSV parser.FPATis a GNU awk feature and is not available in mawk or BSD awk.
RememberField numbering starts at $1,
$0is the whole line, and$NFis always the last field no matter how many there are.
Frequently Asked Questions
What does awk do that grep and cut cannot do together?
awk filters rows, picks and reorders fields, does arithmetic, and keeps state across lines in variables and arrays, all in one pass. A command like `awk -F, '$3 > 100 {sum += $3} END {print sum}' data.csv` sums a column conditionally without extra tools.
When should I use awk instead of sed or grep?
Use grep to find lines, sed to rewrite text, and awk when the data is column-based and you need to select, compare, or total fields. If the task starts with "get the third column where the second column is...", that is awk.
Why does my awk comparison give the wrong result on numbers like 9 and 10?
Fields that look like text are compared as strings, so "9" sorts after "10". Add zero (`$3+0 > 80`) to force a numeric comparison.