awk command snippets

Copy-paste-ready awk one-liners for column extraction, filtering, summing, and log parsing.

awk command snippets

Copy-paste-ready awk one-liners for column extraction, filtering, summing, and log parsing.

awk extracts the second column from whitespace-separated output:

ps aux | awk '{print $2}'

awk uses $NF to reference the last field regardless of how many fields exist:

awk '{print $NF}' file.txt

Sum a Numeric Column with awk

awk accumulates values from column 5 and prints the total:

awk '{sum += $5} END {print sum}' data.txt

Count Lines Matching a Pattern with awk

awk counts lines containing "error":

awk '/error/ {count++} END {print count}' /var/log/syslog

awk filters lines where the third field is greater than 1000:

awk '$3 > 1000' data.txt

Remove Duplicate Lines with awk

awk prints each unique line once using an associative array:

awk '!seen[$0]++' file.txt

awk prepends the line number to each line:

awk '{print NR, $0}' file.txt

Convert Comma-Separated to Tab-Separated with awk

awk replaces commas with tabs:

awk -F, 'BEGIN {OFS="\t"} {$1=$1; print}' data.csv