When to use awk
Use awk for record-based text processing, arithmetic and field selection. Set the separator to match the actual input format and quote the program so the shell does not expand awk expressions.
Syntax
awk [OPTIONS] 'PROGRAM' [FILE...]Square brackets mark optional arguments; an ellipsis means repetition. Replace example file names, user names and server addresses with your own values.
Common options and arguments
Usage examples
Show names from passwd
awk -F: '{print $1}' /etc/passwdEach colon-separated account record produces its first field, the account name.
Sum the first column
awk '{sum += $1} END {print sum}' ./numbers.txtValues in the first whitespace-separated field are accumulated and printed once at the end.
Select rows by value
awk -F, '$3 > 100 {print $1, $3}' ./report.csvRows with a third field above the threshold print selected fields; this simple parser assumes uncomplicated comma-separated data.
Notes and limitations
- The quotes protect the awk program from shell substitutions.
- Separating by comma does not fully parse CSV with quotes and multiline fields.
Common questions and troubleshooting
Can this parse every CSV file?
No. Quoted commas, escaped quotes and multiline records require a CSV-aware parser. A comma field separator alone does not implement CSV rules.
Why does a field number disappear in my command?
Double-quoting an awk program can let the shell expand dollar expressions. Single-quote the program unless shell interpolation is deliberate.
Further help
Options vary by version and distribution. Check the installed documentation with this command: man awk.