awk — Text Processing Language
awk is a complete programming language for processing column-based text data.
Basic syntax
awk 'pattern { action }' file
Print columns
awk '{print $1}' file.txt # Print first column
awk '{print $1, $3}' file.txt # Print columns 1 and 3
awk '{print $NF}' file.txt # Print last column ($NF = number of fields)
awk '{print NR, $0}' file.txt # Print line number and whole line
Custom delimiters
awk -F: '{print $1}' /etc/passwd # Use : as separator
awk -F, '{print $2}' data.csv # CSV processing
Conditions
awk '$3 > 100' file.txt # Lines where col3 > 100
awk '/pattern/' file.txt # Lines matching pattern
awk 'NR==5' file.txt # Line 5 only
awk 'NR>=2 && NR<=5' file.txt # Lines 2-5
Built-in variables
| Variable | Meaning |
|---|---|
$0 | Entire line |
$1..$n | Column 1..n |
NF | Number of fields |
NR | Current line number |
FS | Field separator |
Practical examples
# Sum values in column 3
awk '{sum += $3} END {print sum}' file.txt
# Print username and UID from /etc/passwd
awk -F: '{print $1, $3}' /etc/passwd
# Average of column 2
awk '{sum+=$2; count++} END {print sum/count}' data.txt