awk Cheatsheet
The most used awk commands, ready to copy and paste.
Fill in your values
Enter your own values below and every matching command updates automatically.
Print the first column
awk '{print $1}' <File Name>
Print a specific column by number
awk '{print $<Column Number>}' <File Name>
Print multiple columns
awk '{print $1, $3}' <File Name>
Use a custom field separator
awk -F'<Field Separator>' '{print $1}' <File Name>
Print lines matching a pattern
awk '/<Pattern>/' <File Name>
Print lines that do NOT match a pattern
awk '!/<Pattern>/' <File Name>
Print the number of fields on each line
awk '{print NF}' <File Name>
Print the total number of lines
awk 'END{print NR}' <File Name>
Print each line with its line number
awk '{print NR, $0}' <File Name>
Print the last column
awk '{print $NF}' <File Name>
Sum the values in a column
awk '{sum += $<Column Number>} END {print sum}' <File Name>
Print lines where a column matches an exact value
awk -F'<Field Separator>' '$<Column Number> == "<Pattern>"' <File Name>
Print rows where a numeric column exceeds a value
awk '$<Column Number> > 100' <File Name>
Print unique values from a column
awk '{print $1}' <File Name> | sort -u
Count occurrences of each value in a column
awk '{count[$1]++} END {for (v in count) print v, count[v]}' <File Name>
Print everything between two matching patterns
awk '/start/,/end/' <File Name>
Set the output field separator
awk -F'<Field Separator>' 'BEGIN{OFS="\t"} {print $1, $2}' <File Name>
No commands match your search.