awk flags & options
-F
Set the field separator. Default is whitespace.
awk -F: '{print $1}' /etc/passwd
awk -F, '{print $2}' data.csv
-v
Set a variable before execution begins.
awk -v threshold=100 '$1 > threshold' data.txt
-f
Read the awk program from a file instead of the command line.
awk -f script.awk data.txt
{print}
Print the entire line, or specific fields ($1, $2, etc.).
awk '{print $1}' file.txt
awk '{print $1, $3}' file.txt
$NF
NF is the number of fields. $NF is the last field on the line.
awk '{print $NF}' file.txt
NR
NR is the current line number. Use it to filter by line.
awk 'NR==5' file.txt
awk 'NR>=10 && NR<=20' file.txt
/pattern/
Process only lines matching a regular expression.
awk '/error/' log.txt
awk '/^[0-9]/' file.txt
BEGIN, END
Run code before the first line or after the last line.
awk 'BEGIN{sum=0} {sum+=$1} END{print sum}' numbers.txt
OFS
Output field separator. Controls what goes between printed fields.
awk -F: -v OFS=, '{print $1, $3}' /etc/passwd
Conditional logic
Use if/else and comparison operators.
awk '{if ($3 > 100) print $1, "high"; else print $1, "low"}' data.txt