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.
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
Try It
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
Try It
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
Try It
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
Try It
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
Try It
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
Try It
Press Run to execute
Built-in Functions
AWK provides many built-in functions for string manipulation, math, and I/O.
Syntax
Try It
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
Try It
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
Try It
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
Try It
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
Try It
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
Try It
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
Try It
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.