Wed 10 Aug 2016


Awk syntax looks like pattern { action }.

Awk goes through lines. If the pattern matches a line, the action happens.

We refer to input columns using $N where N is the column number.


#!/usr/bin/awk -f will let you make a .awk file into a script that you can run, passing it the file to be processed.


print prints things.

if (condition) statement else other

while (condition) statement

for (epxression; condition; expression) statement

for (x in arr) statement


BEGIN and END are special patterns that happen before and after each line.


Basic arithmatic works in a C-like way.

The spacebar is string concatenate.


= ! > >= < <= all work as normal.

~ and !~ mean regex match and not-match.

&& || ! work as normal.


Variables are assigned and referenced by name.

a = 3 print a

Assignment can be combined with an operator, as in C.

Special Variables

NF number of fields

NR line number

FS field separator

RS row separator

ORS output row separator

FILENAME current file


Awk doesn't expand variables inside strings.

It does do escaping.