Home·Terminal commands

awk

Text processing

Usage

awk '{print $1}' [file]

Splits each line into fields and runs a little program over them; $0 is the whole line and $1 the first field, and the default separator is any run of whitespace, not one space.

Common flags

FlagMeaning
-FSet the field separator, as in -F,
-vPass a shell value in as an awk variable
-fRead the program from a file
$0The whole line; $1 is the first field, NF the field count

Examples

awk -F, '{print $2}' data.csv

Second column of a comma-separated file.

awk '$3 > 100' access.log

Only the rows whose third field exceeds 100.

awk '{s+=$1} END {print s}' nums.txt

Adds up the first column.

These were built to be piped together. Three joined commands are usually shorter than one that does everything.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does awk do?

Splits each line into fields and runs a little program over them; $0 is the whole line and $1 the first field, and the default separator is any run of whitespace, not one space.

Q. How do I type it?

awk '{print $1}' [file] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

4 are listed here; the full set is in man awk. This command sits under Text processing.

Related commands

man page: man awk