Wawk Language Specification

A complete reference for the Wawk language — POSIX AWK fundamentals plus Wawk extensions. Every section includes a runnable example. Edit the code and press Run to execute it as real WebAssembly in your browser.

Part 1: POSIX AWK Part 2: Wawk Extensions

Patterns

Every AWK program consists of pattern-action pairs. A pattern selects which input lines to process. An action executes when the pattern matches. If no pattern is given, the action runs for every line.

Syntax

/regex/ { action } expression { action } pattern1, pattern2 { action } # range { action } # every line

Try It

Patterns
Loading...
script.awk
input.txt
stdout
Press Run to execute

Fields & Records

AWK splits each input line (record) into fields automatically. $1 is the first field, $2 the second, and so on. $0 is the entire line. NF holds the number of fields, NR the record number.

Syntax

$1 # first field $NF # last field $0 # entire record NF # field count NR # record number FS # input field separator (default: space) OFS # output field separator (default: space)

Try It

Fields
Loading...
script.awk
input.txt
stdout
Press Run to execute

Variables & Assignment

AWK variables are dynamically typed. They hold numbers or strings depending on context. Variables are initialized to 0 or empty string automatically.

Syntax

x = 42 # numeric name = "Alice" # string x = x + 1 # increment $1 = toupper($1) # modify field

Try It

Variables
Loading...
script.awk
input.txt
stdout
Press Run to execute

Regular Expressions

AWK uses extended regular expressions. Patterns between /.../ are regex matches against $0. The ~ and !~ operators test specific fields.

Syntax

/^[A-Z]/ # starts with uppercase /error|warn/ # alternation $1 ~ /^[0-9]+$/ # field matches digits $2 !~ /tmp/ # field does not match

Try It

Regex
Loading...
script.awk
input.txt
stdout
Press Run to execute

BEGIN & END

BEGIN runs once before any input is read. END runs once after all input is processed. Use them for initialization, headers, summaries, and final calculations.

Syntax

BEGIN { # init, print header } /pattern/ { # process each line } END { # summary, print footer }

Try It

BEGIN/END
Loading...
script.awk
input.txt
stdout
Press Run to execute

Associative Arrays

AWK arrays are hash maps. Indices can be any string. Use for (key in array) to iterate. Common patterns: word count, group-by, frequency analysis.

Syntax

arr[key] = value # set arr[key]++ # increment key in arr # test membership delete arr[key] # remove element for (k in arr) ... # iterate

Try It

Arrays
Loading...
script.awk
input.txt
stdout
Press Run to execute

Built-in Functions

AWK provides many built-in functions for string manipulation, math, and I/O.

Syntax

length(s) # string length substr(s, start, len) # substring index(s, t) # position of t in s split(s, arr, fs) # split string sprintf(fmt, ...) # format string toupper(s) / tolower(s) int(x) # truncate to integer sqrt(x) # square root rand() # random [0,1)

Try It

Functions
Loading...
script.awk
input.txt
stdout
Press Run to execute

User-Defined Functions

Define custom functions with the function keyword. Functions can take parameters and return values. Local variables are declared as extra parameters (convention).

Syntax

function name(params) { # body return value } # Local variables: extra params function max(a, b, _tmp) { return (a > b) ? a : b }

Try It

User Functions
Loading...
script.awk
input.txt
stdout
Press Run to execute

Control Flow

AWK supports if/else, while, for, and do-while loops. break and continue work as expected. The ternary operator is available for inline conditionals.

Syntax

if (cond) stmt if (cond) stmt else stmt while (cond) stmt for (init; test; incr) stmt do stmt while (cond) break / continue cond ? val1 : val2

Try It

Control Flow
Loading...
script.awk
input.txt
stdout
Press Run to execute

Output & Formatting

print outputs fields separated by OFS, followed by ORS (newline). printf gives C-style formatting. Output can be redirected to files or pipes within AWK.

Syntax

print expr # print with ORS print a, b, c # separated by OFS printf fmt, args # formatted output print > "file" # redirect to file print | "cmd" # pipe to command

Try It

Output
Loading...
script.awk
input.txt
stdout
Press Run to execute

JSON-Native Records

Wawk makes JSON truly native to AWK. JSON records are auto-detected on input. Access object fields with $.field notation and array elements with $1, $2 positional access.

Syntax

# JSON is auto-detected — no import needed # Access fields with $.field notation { print $.name, $.age } # Array positional access $1, $2, $3 # array element by position print # auto-serializes JSON back to JSON

Try It

JSON-Native
Loading...
script.awk
input.txt
stdout
Press Run to execute

Nested JSON Access

Navigate nested JSON with chained dot-access: $.address.city. Array elements are accessible positionally: $1, $2, etc. print on JSON values auto-serializes back to JSON.

Syntax

$.name # top-level field $.address.city # nested field $1, $2, $3 # array positional access

Try It

Nested JSON
Loading...
script.awk
input.txt
stdout
Press Run to execute

@plugin Directive

The @plugin directive loads Wasm plugins directly in AWK scripts. Plugin functions become available as regular AWK function calls. See the Plugins page for the full development guide.

Syntax

@plugin "my_plugin.wasm" # Plugin functions are called like built-ins { print greet($1) }

Try It

@plugin
Loading...
script.awk
input.txt
stdout
Press Run to execute

wawk-hello: A Complete Example

wawk-hello is the canonical example plugin. It demonstrates the full plugin lifecycle — define the WIT interface, implement the handler, build to Wasm, and load it into AWK scripts.

FunctionArgsDescription
greet(name) 1 Returns "Hello, {name}!"
greet_lang(name) 1 Returns a greeting in one of 10 languages

Clone and Build

$ git clone https://github.com/ailurlabs/wawk-hello.git $ cd wawk-hello $ ./build.sh # Output: wawk-hello.wasm (~60 KB)

Run with wawk

# Basic greeting $ wawk --plugin ./wawk-hello.wasm 'BEGIN { print greet("World") }' Hello, World! # Multi-language greeting $ wawk --plugin ./wawk-hello.wasm 'BEGIN { print greet_lang("Alice") }' Hola, Alice! # Process input data $ echo -e "Alice\nBob\nCharlie" | \ wawk --plugin ./wawk-hello.wasm '{ print greet($1) }' Hello, Alice! Hello, Bob! Hello, Charlie!