awk command
Process columns of text: print fields, filter rows and do sums.
- Text processing
- Package gawk / mawk
- 6 options · 8 examples
Syntax
awk [options] 'pattern { action }' [file…]Provided by the gawk / mawk package. Run man awk for the full manual.
Common options
| Option | What it does |
|---|---|
-F SEP | Field separator, e.g. -F: or -F, |
-v VAR=VAL | Pass a variable into the program |
$1, $2 … | Fields; $0 is the whole line |
$NF | The last field |
NR | Current line number |
NF | Number of fields on the line |
Examples
Print the first column
awk '{print $1}' access.logUse a different separator
awk -F: '{print $1}' /etc/passwdPrint the last column
awk '{print $NF}' file.txtRows where column 3 is above 100
awk '$3 > 100' data.txtSum a column
awk '{sum += $5} END {print sum}' sizes.txtCount requests per IP
awk '{print $1}' access.log | sort | uniq -c | sort -rn | headPrint lines 5 to 10
awk 'NR>=5 && NR<=10' file.txtPrint fields with a custom format
awk -F, '{printf "%-20s %s\n", $1, $2}' users.csv
No example matches. Check the options table or search the guides.
awk guides
- AWK Arithmetic Operations: A Beginner’s Guide to Basic Calculation Methods Linux Commands · Updated Apr 2025
- Getting Started with AWK Linux Commands · Updated Apr 2025
- Awk – Advanced features Linux Commands · Updated Apr 2025
- Conditional Statements in Awk Linux Commands · Updated Apr 2025
- Understand String functions in AWK Linux Commands · Updated Apr 2025
- Understanding Regular Expressions in Awk Linux · Updated Apr 2025
- Using awk, bc and expr to perform Math calculations in Bash Shell Scripting · Updated Apr 2025
