awk Cheatsheet

Basic operations

Print the first column of a file
awk '{print $1}' file.txt
Print the second and third column of a file
awk '{print $2, $3}' file.txt
Print all lines that contain a specific word
awk '/word/ {print}' file.txt
Print the sum of the first column of a file
awk '{sum+=$1} END {print sum}' file.txt
Print the average of the first column of a file
awk '{sum+=$1; count++} END {print sum/count}' file.txt
Print the maximum value of the first column of a file
awk '{if($1>max) max=$1} END {print max}' file.txt
Print the minimum value of the first column of a file
awk '{if($1<min) min=$1} END {print min}' file.txt
Print the number of lines in a file
awk 'END {print NR}' file.txt
Print the number of fields in each line of a file
awk '{print NF}' file.txt
Print specific lines based on a condition
awk '$1 > 50 {print}' file.txt