The peek() function returns a value of TRUE or FALSE depending on whether the pattern passed to it matches the current input string. Use of the peek() function does not advance the starting position, so input can be scanned before other pattern matching. The peek() function stops scanning for a pattern when it reaches a marker.

peek(<pattern>)

The following script looks for the word horse in each input string. The double-dot operator is necessary in the pattern so that the peek() function will match any occurrence of the word horse in a pattern and not just when it appears first.

If the word horse is found in the input string, the peek() function is TRUE and the rule GETLINE executes. GETLINE assigns every word in the string to the variable a. The value of a is printed and the peek() function starts with the next input string. If the peek() function is FALSE, the rule GETLINE does not execute, but the DEFAULT rule does execute.

ASL Script (peek_match.asl):
START {
 peek(..'horse') GETLINE
}
GETLINE{
 a:rep(word) eol
}
do {
 print("Horse found: ".a);
}
DEFAULT {
 ..eol
}
do {
 print("Failed match");
}
Input (peek_match.txt):
moose horse camel
elephant mule camel
goat llama horse
horse goat llama
Output:
$ sm_adapter --file=peek_match.txt peek_match.asl
Horse found: moose horse camel
Failed match
Horse found: goat llama horse
Horse found: horse goat llama
$