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))
awk 'pattern { action }' file
awk -F'column_delimiter' 'pattern { action }' file
pattern
can be a regex, a condition, or both.{ action }
is what to do when the pattern matches.$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)
F','
for CSV files.awk -F',' '{ print $1 }' data.csv
# 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
&&
(AND) or ||
(OR).awk '$1=="foo" && $2 > 10' file