Viewing and processing text

Command awk

Process text by columns and patterns

Linux · Bash examples · commands run on the selected server

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

-F SEPARATOR
Set the field separator.
-v NAME=VALUE
Pass variable to program awk.
$N
Refer to field N; $0 contains the entire line.

Usage examples

Show names from passwd

awk -F: '{print $1}' /etc/passwd

Each colon-separated account record produces its first field, the account name.

Sum the first column

awk '{sum += $1} END {print sum}' ./numbers.txt

Values 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.csv

Rows 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.