Awk Cheatsheet

Text processing, field extraction, patterns & built-in variables

Linux / Command
Contents
📝

Basics

# Basic syntax
awk 'pattern { action }' file.txt
awk '{ print }' file.txt          # print all lines
awk '{ print $0 }' file.txt       # same, $0 = whole line

# Custom field separator
awk -F":" '{ print $1 }' /etc/passwd
awk -F"," '{ print $1, $3 }' data.csv

# From pipe
echo "hello world" | awk '{ print $2 }'
ps aux | awk '{ print $2, $11 }'
📊

Fields & Columns

# $1, $2, ... = fields; $0 = whole line
awk '{ print $1 }' file.txt       # 1st field
awk '{ print $NF }' file.txt      # last field
awk '{ print $(NF-1) }' file.txt  # 2nd to last

# Custom output separator
awk -F":" -v OFS="," '{ print $1, $3 }' /etc/passwd

# Reformat fields
awk '{ print $2, $1 }' file.txt  # swap columns

# Print range of fields
awk '{ for(i=2;i<=NF;i++) printf "%s ", $i; print "" }' file.txt
🔍

Patterns & Conditions

# Pattern matching
awk '/error/' file.txt            # lines containing "error"
awk '!/error/' file.txt           # NOT containing
awk '/^#/' file.txt               # starting with #

# Conditions
awk '$3 > 100' data.txt          # 3rd field > 100
awk '$1 == "admin"' data.txt     # exact match
awk 'NR > 1' data.txt             # skip header
awk 'NR==5,NR==10' file.txt      # lines 5-10
awk 'NF > 0' file.txt             # non-empty lines

# BEGIN / END
awk 'BEGIN { print "Header" } { print } END { print "Done" }' file.txt
📋

Built-in Variables

NR    # current line number (total)
FNR   # line number in current file
NF    # number of fields
FS    # field separator (default: space)
OFS   # output field separator
RS    # record separator (default: newline)
ORS   # output record separator
FILENAME # current filename

# Set in code
awk 'BEGIN { FS=":" ; OFS="," } { print $1,$3 }' /etc/passwd

Actions & Control

# Arithmetic
awk '{ sum += $1 } END { print sum }' nums.txt
awk '{ sum += $3; n++ } END { print sum/n }' data.txt

# Conditional
awk '{ if ($3 > 50) print $1, "high"; else print $1, "low" }' data.txt

# Loop
awk '{ for(i=1; i<=NF; i++) print $i }' file.txt

# Printf
awk '{ printf "%-10s %5d\n", $1, $2 }' data.txt

# Arrays
awk '{ count[$1]++ } END { for(k in count) print k, count[k] }' log.txt
🔧

String Functions

length(s)        # string length
substr(s,start,len) # substring
index(s,t)       # position of t in s
split(s,a,sep)   # split string into array
gsub(r,s)        # global replace (regex)
sub(r,s)         # replace first match
match(s,r)       # regex match
tolower(s)       # lowercase
toupper(s)       # uppercase
sprintf(fmt,...) # formatted string

# Example
awk '{ gsub(/error/, "ERROR"); print }' log.txt
💡

Common Examples

# Sum column
awk '{ s+=$1 } END { print s }' nums.txt

# Unique values
awk '!seen[$0]++' file.txt

# CSV to TSV
awk -F"," -v OFS="\t" '{$1=$1; print}' data.csv

# Top processes by memory
ps aux | awk 'NR>1 { print $4, $11 }' | sort -rn | head

# Word frequency
awk '{ for(i=1;i<=NF;i++) w[$i]++ } END { for(k in w) print w[k],k }' file.txt | sort -rn