AWK Basics — Pattern Scanning and Text Processing in Linux

awk is a pattern-scanning and text-processing language, commonly used for extracting and reformatting columns of data.

Published September 30, 2026

awk reads input line by line, splits it into fields ($1, $2, ...) and runs actions on lines matching patterns. It is ideal for column-based text like logs and CSV.

How it works

The model is pattern { action }. $0 is the whole line, NF the number of fields and NR the line number. Change the separator with -F.

Awk has variables, arrays and arithmetic, so it can total and group data.

Try it

ls -l | awk '{print $5, $9}'
1200 app.js
340 db.js

Practical examples

Print columns

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

Custom separator

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

Sum a column

awk '{sum += $3} END {print sum}' sales.txt

Filter rows

awk '$3 > 100 {print $1}' sales.txt

Common mistakes

  • Wrong field separator.
  • Forgetting quotes around the awk program.
  • Using awk on very complex parsing that needs a real parser.

Key takeaways

  • pattern { action } per line.
  • Fields are $1...$NF.
  • END blocks summarise.

FAQ

awk vs cut?

cut only extracts fields; awk can compute and filter.

Count by key?

awk '{c[$1]++} END {for (k in c) print k, c[k]}' file.

More Linux articles