Grok syntax guide

The essentials

Statements and expressions. A Grok program consists of a sequence of statements and/or expressions. Statements are executed for their side effects, typically changing the value of a variable, printing something to the screen, etc. Expressions typically produce no side effects; instead, they are evaluated and the results of evaluation are printed to standard output (which is typically the screen, unless the output has been redirected).

Grok does not have semicolons at the end of its statements.

Variables and types. There is no need to declare variables in Grok. A variable is automatically created when it is first assigned to. In Grok there are three basic types: strings (including numbers), relations and sets.
All variables have a type, which is automatically determined based on the value assigned to a variable.

Parameters. Grok scripts can take command-line parameters. The parameters can be accessed through the variables $0, $1, $2, and so on. $0 holds the name of the Grok script being executed, $1 holds the first parameter, $2 holds the second parameter, and so on.

Assignment statements

Assignment statements. The assignment statements in Grok have the general form of:

variable := expression

$ variable := expression


In the second of these assignment statements, the assigned value is stored in the variable whose name is in the variable in $ variable

Flow control statements

If statement. The if statement in Grok has the general form of:

if expression then
statements
{elsif expression then
statements }
[ else
statements ]
end if


The expressions must evaluate to a boolean (true or false).

Loop statement. The loop statement in Grok looks as follows:

loop
statements
exit when expression
statements
end loop

The statements in the loop are repeated until the expression evaluates to true.

For statement. The for loop in Grok looks as follows:

for variable in set_expression
statements
end for


During execution, the loop variable will take on all the values in the set in turn. The body of the loop will be executed once for every member in the set, as it appeared before the loop began executing. This means that adding or deleting items from the set in the body of the loop does not affect the number of times the loop is executed.