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 }' fileCommon 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 separatorHow it works: fields, $0 and END
awksplits each line into fields by whitespace:$1is the first field,$2the second,$0the whole line,NFthe 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 blocksBEGIN { }andEND { }fire before and after all lines -ENDis where you print the final total. Use-Fto 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.