Awk Cheat Sheet¶
Process and transform structured text field by field.
Basic Syntax¶
awk [OPTIONS] 'program' [FILE...]
awk -F: 'program' [FILE...] # set field separator to :
awk -v var=value 'program' [FILE...] # pass variable from shell
Built-in Variables¶
| Variable | Meaning |
|---|---|
$0 |
Entire current line |
$1, $2, $NF |
Field 1, field 2, last field |
NF |
Number of fields in current record |
NR |
Current record (line) number |
FNR |
Record number within current file |
FS |
Field separator (default: whitespace) |
OFS |
Output field separator (default: space) |
RS |
Record separator (default: newline) |
ORS |
Output record separator |
FILENAME |
Name of current input file |
Program Structure¶
BEGIN { setup } # runs once before input
/pattern/ { action } # runs when pattern matches
{ action } # runs for every line
END { summary } # runs once after all input
Common Patterns¶
# Print specific field
awk '{print $1}' file.txt
awk -F: '{print $1, $7}' /etc/passwd # fields 1 and 7, space-separated
awk -F: '{print $1 ":" $7}' /etc/passwd # with custom separator
# Conditional
awk '$3 > 1000 {print $1}' /etc/passwd # lines where field 3 > 1000
awk '/error/ {print NR, $0}' log.txt # numbered error lines
awk 'NF > 3' data.txt # lines with more than 3 fields
# Sum a column
awk '{sum += $1} END {print sum}' numbers.txt
# Count matches
awk '/error/ {count++} END {print count}' log.txt
# Average
awk '{sum += $1; n++} END {print sum/n}' numbers.txt
# Print last field
awk '{print $NF}' file.txt
# Print all except first field
awk '{$1=""; print $0}' file.txt
# Format output with printf
awk '{printf "%-20s %5d\n", $1, $2}' data.txt
Text Transformation¶
# Swap fields
awk '{print $2, $1}' file.txt
# Change delimiter (CSV to TSV)
awk -F, '{print $1 "\t" $2 "\t" $3}' data.csv
# Add header line
awk 'BEGIN {print "NAME UID SHELL"} -F: {print $1, $3, $7}' /etc/passwd
# Skip header and process data
awk 'NR > 1 {print $1, $2}' data.csv
# Print lines between two patterns
awk '/START/,/END/' file.txt
Associative Arrays (awk maps)¶
# Count by category
awk '{count[$2]++} END {for (k in count) print k, count[k]}' data.txt
# Find unique values in field 1
awk '!seen[$1]++' file.txt
# Accumulate per key
awk '{sum[$1] += $2} END {for (k in sum) print k, sum[k]}' data.txt
Common Recipes¶
# Lines longer than 80 characters
awk 'length($0) > 80' file.txt
# Remove duplicate lines (keeping order)
awk '!seen[$0]++' file.txt
# Count requests per IP in access log
awk '{print $1}' access.log | sort | uniq -c | sort -rn | head -10
# Sum file sizes from ls -l
ls -l | awk 'NR > 1 {sum += $5} END {print "Total:", sum, "bytes"}'
# Print lines where field 2 matches pattern
awk '$2 ~ /pattern/' data.txt
# Print lines where field 2 does NOT match
awk '$2 !~ /pattern/' data.txt
Related: grep, sed, pipes-redirection