C.W.K.
Stream
Lesson 04 of 10 · published

awk: Pattern-Action Language

~15 min · awk, fields, scripting

Level 0Window Tourist
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

A small language inside one command

awk reads input line by line and runs a program. The program is a sequence of pattern { action } rules. The pattern decides which lines fire the action; the action is anything you can write in awk's C-like mini-language.

Field variables

For each line, awk auto-splits on whitespace into $1, $2, ..., $NF (the last). NF is the number of fields, NR is the current line number. $0 is the whole line.

Examples that earn their keep

  • awk '{print $1}' — first field of each line.
  • awk '{print $NF}' — last field.
  • awk -F: '{print $1}' /etc/passwd — change delimiter to colon.
  • awk '/error/ {print NR, $0}' — line number + content of lines containing "error".
  • awk 'NR==5' — print only line 5.
  • awk '{sum += $3} END {print sum}' — sum the third column.
  • awk 'length($0) > 100' — show lines longer than 100 chars.

BEGIN and END blocks

BEGIN { ... } runs once before any input. END { ... } runs once after. Useful for headers, totals, averages.

awk 'BEGIN {print "size,name"} {print $5","$9}' <(ls -l)

That's a CSV header plus size+name per file — pipeable into sort or jq.

Why awk over sed

sed is great for substitution. awk is great for arithmetic and field manipulation. Anytime you find yourself counting cells, summing columns, or filtering by numeric comparison, switch from sed to awk.

Code

Top processes by CPU·bash
ps aux | awk 'NR>1 {print $3,$11}' | sort -rn | head
# Sum disk usage shown by ls -l
ls -l ~/Downloads | awk '{s+=$5} END {print s/1024/1024 "MB"}'
Reformat CSV-ish data·bash
awk -F: '{printf "%-20s -> shell %s\n", $1, $7}' /etc/passwd | head
# Filter by numeric field
awk -F, '$3 > 100 {print}' big.csv

External links

Exercise

List your top 5 CPU-hogging processes: ps aux | awk 'NR>1 {print $3,$11}' | sort -rn | head -5. Then total Downloads size: ls -l ~/Downloads | awk '{s+=$5} END {print s/1024/1024 "MB"}'. Then count blank lines in a file: awk '/^$/ {c++} END {print c}' file.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.