Pull out columns, filter rows and do quick maths on text. Tap any command to copy it.
awk '{print $1}' file.txtPrint the first field of each lineawk '{print $1, $3}' file.txtPrint fields 1 and 3awk '{print $NF}' file.txtPrint the last field on each lineawk '{print NR, $0}' file.txtPrefix each line with its line numberawk -F',' '{print $2}' file.csvUse a comma as the field separatorawk -F':' '{print $1}' /etc/passwdSplit on colons (print usernames)awk '/pattern/' file.txtPrint lines matching a patternawk '$3 > 100' file.txtPrint rows where field 3 exceeds 100awk 'NR==1' file.txtPrint just the first line (the header)awk 'NR>1' file.txtSkip the header lineawk 'length > 80' file.txtPrint lines longer than 80 charactersawk 'NF' file.txtPrint only non-empty linesawk '{sum += $1} END {print sum}' fSum the first columnawk '{print $1+$2}' file.txtAdd two columns per lineawk 'END {print NR}' file.txtCount lines (like wc -l)awk '!seen[$0]++' file.txtRemove duplicate lines, keep the orderawk '{a+=$1} END {print a/NR}' fAverage the first columnWhere grep matches lines and sed edits them, awk treats each line as a set of fields — columns split on whitespace by default. $1 is the first column, $2 the second, and $NF the last (NF being the number of fields). So awk '{print $1}' pulls the first column out of any whitespace-separated data — log files, command output, tables — and awk '{print $1, $NF}' grabs the first and last. For CSVs or /etc/passwd, change the separator with -F',' or -F':'.
An awk program is a series of pattern { action } pairs: for each line, if the pattern is true, the action runs. Leave out the action and awk prints matching lines, so awk '$3 > 100' prints every row whose third column is over 100 — numeric comparisons that grep can't do. The built-in variable NR (record number) makes header handling trivial: NR>1 skips the first line, and NR==1 keeps only it.
awk's END block runs once after all lines are read, which is perfect for totals: awk '{sum += $1} END {print sum}' adds up a column, and dividing by NR gives an average — a one-liner that replaces opening a spreadsheet. One famous idiom, awk '!seen[$0]++', removes duplicate lines while preserving their original order, something sort -u can't do. When you need to edit text rather than analyse it, reach for sed; to filter before processing, pipe through grep.