String templates in expressions

A string template is a string literal that includes one or more embedded expressions. Use string templates when you want a more readable result for your formatted strings.

When a string template is resolved, the embedded expressions are replaced by the string representations of the expression results.

For more information about string literals and expressions, see Types of expressions.

Syntax

The syntax for an embedded expression starts with the dollar sign ( $ ) and uses curly braces { } to enclose the expression. For example: ${expression}.

Examples using field names in string templates

Consider the following data:

status host action
200 www1 purchase
200 www2 purchase
200 www1 addtocart
200 www2 purchase
400 www1
400 www1 purchase
400 www1 addtocart
403 www2 changequantity
404 www1 view
404 www1 purchase
404 www2 changequantity
404 www3 purchase

The following search contains a string template with two expressions, ${status} and ${action}, with a string literal, with, between the expressions. The entire string literal must be enclosed in double quotation marks.

The results look like this:

Here's another example:

The results look something like this:

Examples using functions in string templates

You can specify functions in string template expressions.

Suppose you have fields that contain low and high values, such as those in the following table:

day low high
sun -3 18
mon -4 15
tue -4 -1
wed 8 23
thu 10 30
fri 13 32
sat 9 21

You want to calculate the difference between the values and return the result as an absolute value. Here is an example using a string template with an eval command:

The results look like this:

You can also use string templates in the SELECT clause of the from command. For example, this search includes the avg function in a string template: