Use stats with eval expressions and functions
This topic discusses how to use eval expressions and functions within your stats calculation.
- For more information about the eval command and syntax, see the eval command in the Search Reference.
- For the list of eval functions, see Evaluation functions in the Search Reference.
- Also, you can read more about using the eval command to evaluate and manipulate fields in another section in this manual.
Example 1: Distinct counts of matching events
This example counts the IP addresses where the errors originate. This is similar to a search for events that is filtered for a specific error code, and then used with the stats command to count the IP addresses.
status=404 | stats dc(ip)
The best way to do this with an eval expression is:
status=404 | stats dc(eval(if(status=404, ip, NULL))) AS dc_ip
Example 2: Categorizing and counting fields
Find out how much of your organization's email comes from
.org, or other top level domains.
sourcetype="cisco_esa" mailfrom=* | eval accountname=split(mailfrom,"@")
| eval from_domain=mvindex(accountname,-1)
| stats count(eval(match(from_domain, "[^\n\r\s]+\.com"))) AS ".com",
count(eval(match(from_domain, "[^\n\r\s]+\.net"))) AS ".net",
count(eval(match(from_domain, "[^\n\r\s]+\.org"))) AS ".org",
count(eval(NOT match(from_domain, "[^\n\r\s]+\.(com|net|org)"))) AS "other"
The first half of this search uses the
eval command to break up the email address in the
mailfrom field. The search then defines the
from_domain as the portion of the
mailfrom field after the
The results are then piped into the
stats command. The
count() function is used to count the results of the
eval expression. Here,
eval uses the
match() function to compare the
from_domain to a regular expression that looks for the different suffixes in the domain. If the value of
from_domain matches the regular expression, the
count is updated for each suffix,
.org. Other domain suffixes are counted as
This produces the following results table:
Note: This example used generated email data (
sourcetype=cisco_esa). You should be able to run this example on any email data by replacing the
sourcetype=cisco_esa with your data's
sourcetype value and the
mailfrom field with your data's email address field name (for example, it might be
To, From, or Cc).
Use the stats command and functions
Add sparklines to search results
This documentation applies to the following versions of Splunk® Enterprise: 6.2.0, 6.2.1, 6.2.2, 6.2.3, 6.2.4, 6.2.5, 6.2.6, 6.2.7, 6.2.8, 6.2.9, 6.2.10, 6.2.11, 6.2.12, 6.2.13, 6.3.0, 6.3.1, 6.3.2, 6.3.3, 6.3.4, 6.3.5, 6.3.6, 6.3.7, 6.3.8, 6.3.9, 6.3.10, 6.3.11, 6.4.0, 6.4.1, 6.4.2, 6.4.3, 6.4.4, 6.4.5, 6.4.6, 6.4.7, 6.4.8, 6.5.0, 6.5.1, 6.5.1612 (Splunk Cloud only), 6.5.2, 6.5.3, 6.5.4, 6.5.5, 6.6.0, 6.6.1, 6.6.2, 6.6.3