gawk

Verified for current stable LTS

Gawk Commands

Gawk command syntax with verified terminal examples.

Commands

14 commands for Gawk

gawk

Gawk Command: Conditional Printing Different Values

gawk '{if ($1 == "foo") print "Exact match foo"; else if ($1 ~ "bar") print "Partial match bar"; else print "Baz"}' <path/to/file>
gawk

Gawk Command: Print Every Third Line

gawk 'NR%3==1' <path/to/file>
gawk

Gawk Command: Print Fifth Column

gawk '{print $5}' <path/to/file>
gawk

Gawk Command: Print Fifth Column Space Separated File

gawk '{print $5}' <path/to/file>
gawk

Gawk Command: Print Last Column Comma Field Separator

gawk -F ',' '{print $NF}' <path/to/file>
gawk

Gawk Command: Print Last Column With Comma Separator

gawk -F ',' '{print $NF}' <path/to/file>
gawk

Gawk Command: Print Lines With Tenth Column Value In Range

gawk '($10 >= <min_value> && $10 <= <max_value>)' <path/to/file>
gawk

Gawk Command: Print Lines Within Column Range

gawk '($10 >= <min_value> && $10 <= <max_value>)' <path/to/file>
gawk

Gawk Command: Print Second Column Containing Foo

gawk '/<foo>/ {print $2}' <path/to/file>
gawk

Gawk Command: Print User Table With Header

gawk 'BEGIN {FS=":";printf "% -20s %6s %25s\n", "Name", "UID", "Shell"} $4 >= 1000 {printf "% -20s %6d %25s\n", $1, $4, $7}' /etc/passwd
gawk

Gawk Command: Print Users With Uid Greater Than Or Equal To 1000

gawk 'BEGIN {FS=":";printf "% -20s %6s %25s\n", "Name", "UID", "Shell"} $4 >= 1000 {printf "% -20s %6d %25s\n", $1, $4, $7}' /etc/passwd
gawk

Gawk Command: Print Values Based On Conditions

gawk '{if ($1 == "foo") print "Exact match foo"; else if ($1 ~ "bar") print "Partial match bar"; else print "Baz"}' <path/to/file>
gawk

Gawk Command: Sum First Column Values

gawk '{s+=$1} END {print s}' <path/to/file>
gawk

Gawk Command: Sum First Column Values And Print Total

gawk '{s+=$1} END {print s}' <path/to/file>

Suggest a Gawk Command

Submit missing workflows, corrections, or verified alternatives for this tool.

FAQ

Coverage: Focused examples for common Gawk workflows.

Verified version: current stable LTS.

Verification: Test commands in a disposable workspace and submit notes for edge cases.