awk command (Linux / bash / sh / zsh)
What it does
Process record-oriented text with patterns, fields, and actions. When people search for the awk command linux tutorial, they usually want awk's everyday flags, safe examples, and how it differs from related tools. Below you'll find syntax, options, and copyable examples for Bash/sh/zsh.
Process record-oriented text with patterns, fields, and actions.
Syntax
awk [OPTION] 'PROGRAM' [FILE...]
Common options
| Flag / option | Meaning |
|---|---|
| -F SEP | Set the input field separator. |
| -v VAR=VALUE | Assign a variable before processing. |
| -f FILE | Read the awk program from a file. |
| --posix | Use strict POSIX behavior. |
Practical examples
Print a column
awk -F, '{print $2}' users.csvPrints the second CSV-like field.
Sum values
awk '{sum += $1} END {print sum}' totals.txtAdds the first field from every record.
Filter processes
ps aux | awk '$3 > 50 {print $2, $11}'Shows processes using more than 50 percent CPU.
Common mistakes
- Treating basic field splitting as a complete CSV parser when values can contain quoted commas.
- Using double quotes around the program and accidentally expanding shell variables.
Related commands
- cutExtract selected fields, characters, or bytes from each input line.
- sedTransform text streams with scripted substitutions and edits.
- grepSearch text for lines matching a pattern.
- Where-ObjectFilter pipeline objects by property values or a script block predicate.
- catConcatenate files and write their contents to standard output.
- Get-ContentRead file content or provider data as a stream of objects.
- echoWrite arguments and shell-expanded values to standard output.
- echoDisplay text or control command echoing in a batch script.
FAQ
What does awk do in Linux?
Process record-oriented text with patterns, fields, and actions.
How do I use awk from the terminal?
Start with `awk [OPTION] 'PROGRAM' [FILE...]`, then choose only the options needed for the task.
What is a practical awk example?
Try `awk -F, '{print $2}' users.csv` — Prints the second CSV-like field.
Can I use awk in a shell script?
Yes. awk works in Bash, sh, and Zsh, but scripts should handle quoting, errors, and command output explicitly.
Browse the full Linux and Windows command reference to search more bash, CMD, PowerShell, Conda, and GitHub CLI commands.