Linuxlab

awk command in Linux: field processing

Updated July 3, 2026

The awk command splits each line into fields and processes them by a 'condition { action }' rule.

Syntax

awk 'condition { action }' file

Common cases

awk '{ print $1 }' access.log        # the first field of each line
awk '{ print $1, $4 }' access.log    # the first and fourth
awk '$4==404 { n++ } END { print n }' access.log   # count by a condition
awk -F: '{ print $1 }' /etc/passwd   # -F: a colon field separator

How it works: fields, $0 and END

awk splits each line into fields by whitespace: $1 is the first field, $2 the second, $0 the whole line, NF the number of fields. A program is a set of 'condition { action }' rules: for each line awk tests the condition and, if true, runs the action. The special blocks BEGIN { } and END { } fire before and after all lines - END is where you print the final total. Use -F to change the field separator.

Used in

You need this in the lesson Text: grep, sed, awk. The theory is the chapter Bash in depth.

See also

The grep command, The sed command, Regular expressions.

Try it

Open the sandbox: a real throwaway Linux terminal in your browser. Run the commands above by hand - the container is deleted when you leave.