Skip to main content
Version: 5.2

eval

Description

Performs various operations on the data.

Syntax

| eval <field>=<expression>["," <field>=<expression>]...

Required Arguments

ParameterSyntaxDescription
field<string>Name of the field with the result of the operation.
expression<expression>Operation (calculation) or a set of operations (calculations) to be performed for the new field.

Data Types

eval operates with the following basic data types and attempts to retain the original or assigned data type in the output:

  • string
  • numeric
  • boolean
  • null
  • multivalue
info

multivalue represents arrays of values, where each element is one of the basic data types.

Query examples

Example 1

Creating a field to classify the operation status based on the error code.

In this example, the system performs a conditional check on the errNo field for a match. If the condition is met, it returns the value OK; otherwise, it returns NOTOK.

Example #1
source nix_events-* | eval res = if(errNo == -1, "OK", "NOTOK")

The query execution result may be the following table:

errNoresstatus
-1OKInteger
"-1"OKString
200NOTOKInteger
nullNOTOKnull

Example 2

In this example, the case command classifies orders based on the value of the total_price field and saves the result in status.

Example №2
... | eval status = case(total_price > 3000, "High", total_price > 2000, "Medium", true(), "Low")

The query execution result may be the following table:

usertotal_pricestatus
user14100High
user22315Medium
user3828Low

Example 3

In this example, the system classifies events based on the presence of the pattern "accounting" in the event.action field. It creates a new field action_type, which takes the value "account" if the substring is found, or auth" otherwise.

Example №3
... | eval action_type = if(like(event.action, "%accounting%"), "account", "auth")

The query execution result may be the following table:

host.nameevent.actionaction_type
SPBSpecial loginauth
MSKSystem loginauth
VLGAccount managementaccount

Example 4

In this example, the system counts the number of items in the items field for each record in the dataset.

Example №4
... | eval total_count = mvcount(items)

The query execution result may be the following table:

usertotal_priceitemstotal_count
user14100[ "Gyro", "Chicken nuggets" ]2
user22315[ "Hot dog", "Sweet potato fries", "Caprese salad", "Asian salad", "Fried tilapia fillet", "Crab cakes", "Pepper sausages", "Chicken tacos", "Chicken rice soup" ]9
user3828[ "Pulled pork sandwich", "Pancakes", "Reuben sandwich" ]3

Example 5

Calculating the percentage of successful operations based on the error count and the total number of operations for each host. The round function limits the result of the successful operation share calculation to two decimal places. The result is written to a new field success_rate.

Example №5
... | eval success_rate = round(((total_ops - error_count) / total_ops) * 100, 2)

The query execution result may be the following table:

hosttotal_opserror_countsuccess_rate
server110005095.00
server28002097.50
server3120012090.00

Example 6

This example creates a user identifier in email format based on the user.name and host.name fields. The trim function removes the $SRV characters from both sides of the user.name string. The replace function uses a regular expression where the first group captures the initial part of the name, and the second captures the uppercase letters at the end. The replacement pattern $1.$2 adds a dot between the groups. The lower function converts the result to lowercase, after which the string is concatenated with @ and the value of host.name.

Example №6
... | eval email = lower(replace(trim(user.name, "$SRV"), "^([a-zA-Z]+)([A-Z]+)([A-Z]+)$", "$1.$2$3")) + "@" + host.name

The query execution result may be the following table:

user.namehost.nameemail
SmithAG$SRVserver.localsmith.ag@server.local
TaylorSI$SRVserver.localtaylor.si@server.local
JonesMN$SRVserver.localjones.mn@server.local

Example 7

Calculating the total area of two circles based on their radii. The pi() function returns the value of pi, and the pow() function squares the radius value. The result of summing the areas is written to a new field total_area.

Example №7
... | eval total_area = pi() * pow(radius_a, 2) + pi() * pow(radius_b, 2)

The query execution result may be the following table:

radius_aradius_btotal_area
3.04.078.53981634
2.52.5110.83572895
5.03.539.26990817