AWK Cheat Sheet

AWK, created in 1977 by Aho, Weinberger, and Kernighan, is a fast, versatile tool for text processing and data manipulation. Perfect for extracting fields, filtering lines, and generating reports, it shines with column-based data. Lightweight yet powerful, AWK remains a go-to for efficient scripting in Unix workflows.

flowchart TB
    B((BEGIN block))
    B --> C[Read first line]
    C --> D{Pattern 1?}
    D -->|Match| E[Action 1]
    D -->|No match| F{Pattern 2?}
    F -->|Match| G[Action 2]
    F -->|No match| H{More patterns?}
    H -->|Yes| I[Extra actions...]
    H -->|No| J{Next line?}
    E --> F
    G --> H
    I --> J
    J -->|Yes| C[Read first line]
    J -->|No| K((END block))

1. Overview and Basic Structure

awk 'pattern { action }' file

awk -F'column_delimiter' 'pattern { action }' file

2. Key Variables and Separators

$0      # Entire current line
$1-$n   # Field number (columns)
NF      # Number of fields in the current line
NR      # Current line number
FS      # Field separator (default is whitespace)
RS      # Record separator (default is newline)
OFS     # Output field separator (join columns when printing)

3. Pattern Matching

# Match lines by regex
awk '/regex/' file

# Match by field
awk '$1 == "value"' file

# Match by line number
awk 'NR > 3' file

# Match by line length
awk 'length($0) > 80' file

# Range of patterns
awk '$1 == "start", $1 == "end"' file