Skip to content

Text processing

12 min

The loop awk wrote for you

Patterns, actions, fields, and END.

awk '{print $1}' looks like a magic incantation until you see what is missing from it: the loop. awk reads a record, splits it into fields, tries every pattern, and runs the actions that matched — for every line, without being asked.

A program is a list of pattern { action } rules. A pattern with no action prints the record, which is why awk '/ERROR/' is a grep. An action with no pattern runs on every line.

$0 is the whole record, $1 the first field, $NF the last. NR is the record number.

Try this

The columns in this file do not line up, and it does not matter: the default separator is a run of blanks, not a single space. That is the difference between awk '{print $2}' and cut -c, and it is why the first is what people actually use.

-F sets the separator. A single character is taken literally, so -F: on a::b gives an empty second field; anything longer is a regular expression.

Try this

A pattern can be any expression. $2 > 40 compares the second field as a number, because a field that looks like a number is one — that is awk's strnum rule, and it is why the same comparison against a quoted string would compare text instead.

BEGIN runs before the first record and END after the last, which is where a running total turns into an answer.

Try this

for (k in a) walks the subscripts in no promised order. A script that needs an order pipes the output through sort — the language will not do it for you.

$

Files

The files in the sandbox, with their modes, link counts and sizes
ModeLinksSizeName
-rw-r--r--1101table.txt
-rw-r--r--188totals.csv