Skip to content

AWK Command Cheatsheet

Table of Contents

Basic Syntax

Terminal window
awk 'pattern { action }' file
  • pattern: Condition to match (optional)
  • action: Operation to perform when pattern matches
  • file: Input file (optional, can use stdin)

Built-in Variables

Terminal window
$0 # Entire current line
$1-$n # Field numbers (columns)
NF # Number of fields in current record
NR # Current record (line) number
FNR # Record number in current file
FILENAME # Current input filename

Field Operations

Terminal window
# Print specific fields
awk '{print $1, $3, $NF}' # First, third, and last fields
awk '{print $(NF-1)}' # Second-to-last field
awk '{print NF}' # Number of fields in line
# Field manipulation
awk '{$1=$1; print}' # Force field recalculation
awk '{$2="new"; print}' # Change second field

Text Processing

Terminal window
# Match in specific field
awk '$1 ~ /pattern/' # Pattern in first field
awk '$NF ~ /pattern/' # Pattern in last field
# Multiple patterns
awk '/pattern1/ && /pattern2/'
awk '$1 ~ /pattern/ || $2 > 100'

Advanced Field Processing

Terminal window
# Custom field separator
awk -F',' '{print $1, $2}' # CSV files
awk -F':' '{print $1, $NF}' # System files
# Multiple separators
awk -F'[,:;]' '{print $1, $2}'
# Change separator for output
awk 'BEGIN{OFS=","} {print $1, $2}'

Practical Examples

Log File Processing

Terminal window
# Count HTTP status codes
awk '$9 ~ /^[0-9]/ {codes[$9]++}
END {for (code in codes)
print code, codes[code]}' access.log
# Find error messages
awk '$3 == "ERROR" {print $0}' logfile.txt
# Time-based filtering
awk '$1 > "2023-01-01" {print $0}' dated_log.txt

Data Analysis

Terminal window
# Column statistics
awk '{
sum += $1
sumsq += $1 * $1
}
END {
print "Count:", NR
print "Sum:", sum
print "Average:", sum/NR
print "Std Dev:", sqrt(sumsq/NR - (sum/NR)^2)
}' data.txt
# Frequency count
awk '{count[$1]++}
END {for (word in count)
printf "%-20s %d\n", word, count[word]}'

File Operations

Terminal window
# Numbered output
awk '{printf "%5d : %s\n", NR, $0}'
# Format CSV as table
awk -F',' '{printf "%-20s %-10s %s\n", $1, $2, $3}'
# Remove duplicate lines
awk '!seen[$0]++'