awk - Extract Columns and Transform Text

Learn awk in 5 minutes. Extract columns from files, parse CSVs, and process text data without writing a script.

commandsLast updated 2026-02-24

awk: It's Just Columns

You have a file with data in columns. You only want column 3. How hard can that be?

Harder than it should be, until you learn awk.

The One Command You Need

awk '{print $2}' scores.txt

That's it. That prints the second column of every line. You see the pattern. $1 is the first column, $2 is the second, $3 is the third. You can go home now, thanks for reading.

Still here? Good. Let's go deeper.

How awk Thinks

When awk reads a line, it automatically splits it on whitespace and numbers the pieces. Given this line:

alice 2500 gold

awk sees:

  • $1 = alice
  • $2 = 2500
  • $3 = gold
  • $0 = the whole line

That's the entire mental model. awk splits, you pick which piece you want.

The Three Patterns That Handle 95% of Real Work

Pattern 1: Print a column

awk '{print $1}' file.txt

Pattern 2: Print multiple columns

awk '{print $1, $3}' file.txt

The comma adds a space between them. Without the comma, they smash together.

Pattern 3: Different separator (CSV, logs, etc.)

awk -F, '{print $2}' data.csv

The -F, tells awk "split on commas, not spaces." Use -F: for colons, -F'\t' for tabs.

That's literally it. It's like the 80/20 rule, but better because it's 95/5. This is where I'd tell you to bookmark this page for reference, but you won't need to.

Wading into the Weeds with Filtering

Want column 2, but only from lines containing "error"?

awk '/error/ {print $2}' log.txt

The /error/ part is a filter. Only matching lines run the {print} part.

You can combine this with grep knowledge - same regex rules apply.

Useful Tricks

The following are what make awk scary, but you could also flip the script and say they make awk powerful.

Print the last column (when there are too many to count):

awk '{print $NF}' file.txt

NF means "number of fields." So $NF is "the field at position NF" - aka the last one.

Add line numbers: (when you want awk to count the rows for you)

awk '{print NR, $0}' file.txt

NR is the current line number. $0 is the whole line.

Swap column order: (literally just to confuse your coworker)

awk '{print $2, $1}' file.txt

Uppercase everything: (WHEN YOU ARE YELLING)

awk '{print toupper($0)}' file.txt

Real Example: Parsing a Game Leaderboard

Say you have scores.txt:

alice 2500 warrior
bob 1800 mage
carol 3200 rogue

Get just the names:

awk '{print $1}' scores.txt

Get names and scores:

awk '{print $1, $2}' scores.txt

Find the rogue's score:

awk '/rogue/ {print $2}' scores.txt

When to Use awk vs Other Tools

  • Just searching? Use grep.
  • Simple find/replace? Use sed.
  • Need specific columns? Use awk.
  • Complex transformations? Still awk, but maybe time for a script.

awk and grep are best friends. Grep finds the lines, awk carves them up:

grep "error" log.txt | awk '{print $1, $4}'

Quick Reference

| What you want | Command | |---------------|---------| | First column | awk '{print $1}' file | | Last column | awk '{print $NF}' file | | Multiple columns | awk '{print $1, $3}' file | | CSV column | awk -F, '{print $2}' file | | Filter + extract | awk '/pattern/ {print $2}' file | | Line numbers | awk '{print NR, $0}' file |

Practice

The best way to learn awk is to use it. Try CTF challenges that require parsing log files and extracting data - you'll reach for awk constantly.


Yes, the syntax looks like someone's cat walked across the keyboard. But now you know the secret: it's just print $N with occasional filters. That's awk.