awk command snippets
Copy-paste-ready awk one-liners for column extraction, filtering, summing, and log parsing.
- awk command snippets
- Print a Specific Column with awk
- Print the Last Field of Each Line with awk
- Sum a Numeric Column with awk
- Count Lines Matching a Pattern with awk
- Print Lines Where a Field Exceeds a Threshold with awk
- Remove Duplicate Lines with awk
- Print Line Numbers with awk
- Convert Comma-Separated to Tab-Separated with awk
awk command snippets
Copy-paste-ready awk one-liners for column extraction, filtering, summing, and log parsing.
Print a Specific Column with awk
awk extracts the second column from whitespace-separated output:
ps aux | awk '{print $2}'Print the Last Field of Each Line with awk
awk uses
$NF to reference the last field regardless of how many fields exist:
awk '{print $NF}' file.txtSum a Numeric Column with awk
awk accumulates values from column 5 and prints the total:
awk '{sum += $5} END {print sum}' data.txtCount Lines Matching a Pattern with awk
awk counts lines containing "error":
awk '/error/ {count++} END {print count}' /var/log/syslogPrint Lines Where a Field Exceeds a Threshold with awk
awk filters lines where the third field is greater than 1000:
awk '$3 > 1000' data.txtRemove Duplicate Lines with awk
awk prints each unique line once using an associative array:
awk '!seen[$0]++' file.txtPrint Line Numbers with awk
awk prepends the line number to each line:
awk '{print NR, $0}' file.txtConvert Comma-Separated to Tab-Separated with awk
awk replaces commas with tabs:
awk -F, 'BEGIN {OFS="\t"} {$1=$1; print}' data.csv