Wed 10 Aug 2016

Awk

http://www.grymoire.com/Unix/Awk.html

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.

Execution

#!/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.

Actions

print prints things.

if (condition) statement else other

while (condition) statement

for (epxression; condition; expression) statement

for (x in arr) statement

Keywords

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

Operators

Basic arithmatic works in a C-like way.

The spacebar is string concatenate.

Conditions

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

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

&& || ! work as normal.

Variables

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

Strings

Awk doesn't expand variables inside strings.

It does do escaping.