eval
Compute new fields, or overwrite existing ones, from expressions: arithmetic, string building, conditions and functions.
Use eval to compute a field from an expression for each result row. You can add a field or replace an existing value.
Syntax
... | eval <field>=<expression>[, <field>=<expression> ...]| Argument | Description |
|---|---|
field | Name of the new field. If it already exists, it is overwritten. Quote names with spaces: eval "risk label"=.... |
expression | Fields, literals, operators and functions. |
Assignments run left to right, so a later assignment can use an earlier one: eval a=1, b=a*2.
Expressions
Operators
Listed from lowest to highest precedence:
| Operator | Meaning |
|---|---|
OR, || | Logical or |
AND, && | Logical and |
NOT | Logical not |
IN (…), NOT IN (…) | Membership: http_request.http_method IN ("PUT", "DELETE") |
==, =, !=, >, <, >=, <=, CONTAINS, LIKE | Comparison. Returns 1 or 0. |
+, - | Add, subtract (numbers only) |
*, /, % | Multiply, divide, modulo |
. | String concatenation |
Use parentheses whenever you mix operators.
Literals
- Numbers:
42,3.5 - Strings:
"text"or'text'. A quoted string inevalis always a literal, never a field name. - Booleans:
true,false - Intervals:
INTERVAL 15 MINUTE,INTERVAL 1 DAY - Regex:
/pattern/.field=/re/insideevalis a case-insensitive regex match, as it is inwhere.
Fields
Reference fields by their exact OCSF 1.9 path, unquoted: http_request.url.hostname, traffic.bytes_out. A field that no event populates evaluates to null. See OCSF field reference.
Functions
eval accepts every scalar function on the Functions page: string, encoding, hashing, IP, URL and domain, threat hunting (entropy, defang, edit distance), time, conversion, conditional, math, regex and array functions.
Examples
Turn bytes into kilobytes:
class_uid=4002 | eval kb=round(http_response.length / 1024, 1) | table time, http_request.url.url_string, kbOutbound-to-inbound byte ratio per connection, guarded against division by zero:
class_uid=4001 | eval ratio=round(traffic.bytes_out / (traffic.bytes_in + 1), 2) | where ratio > 10 | table src_endpoint.ip, dst_endpoint.ip, traffic.bytes_out, traffic.bytes_in, ratioBucket HTTP responses into classes:
class_uid=4002 | eval outcome=case(http_response.code >= 500, "server_error", http_response.code >= 400, "client_error", "ok") | stats count by outcomeLabel rows with if:
class_uid=4001 | eval direction=if(is_private_ip(dst_endpoint.ip), "internal", "external") | stats count by directionScore encoded and high-entropy PowerShell:
class_uid=1007 process.name="powershell.exe" | eval encoded=match(process.cmd_line, "(?i)-e(nc|ncodedcommand)?\s"), score=round(entropy(process.cmd_line), 2) | where encoded=1 OR score > 5 | table time, device.hostname, actor.user.name, scorePull the Base64 payload out of an encoded command line:
class_uid=1007 process.cmd_line="*-EncodedCommand*" | eval blob=regex_extract(process.cmd_line, "(?i)-EncodedCommand\s+(\S+)") | stats count by blob | sort -countDefang URLs before you paste them into a ticket:
class_uid=4002 http_response.code=200 | eval safe_url=defang(http_request.url.url_string) | table time, src_endpoint.ip, safe_urlFall back across class-dependent user fields:
metadata.log_source=windows_event | eval who=coalesce(user.name, actor.user.name, "unknown") | stats count by whoHour of day and age of each event:
class_uid=3002 | eval hr=hour(time), day=strftime(time, "%Y-%m-%d"), age_s=age(time) | table time, user.name, hr, day, age_sPercentage of server errors per source, after stats:
class_uid=4002 | stats count(eval(http_response.code >= 500)) as errors, count as total by metadata.log_source | eval error_pct=round(errors * 100 / total, 1)Notes
evalkeeps every row; it only adds or replaces a column. Filter with where.substr()andmvindex()use 0-based positions.- String comparisons with
=,==,!=andCONTAINSare case-insensitive, as inwhere.match()is case-sensitive unless the pattern starts with(?i). base64_decode()returns null for an invalid or empty decoded value. Narrow the rows first, and test on a few events.
More references
- stats: aggregate computed fields