Searching for different values in the same field has been made easier. Thank you Splunk!
For example, suppose in the "error_code" field that you want to locate only the codes 400, 402, 404, and 406.
It is really tedious to have to type field-value pair after field-value pair just to search for a list of values in the same field. But that's exactly what you had to do before version 6.6.0. You had to specify each field-value pair as a separate OR condition.
...error_code=400 OR error_code=402 OR error_code=404 OR error_code=406...
One of the best improvements made to the search command is the IN operator. With the IN operator, you can specify the field and a list of values. For example:
... error_code IN (400, 402, 404, 406) | ...
Because the search command is implied at the beginning of a search string, all you need to specify is the field name and a list of values. The syntax is simple:
field IN (value1, value2, ...)
Note: The IN operator must be in uppercase.
You can also use a wildcard in the value list to search for similar values. For example:
... error_code IN (40*) | ...
This search looks at the error_code field in your events and returns any event with a code that begins with 40.
How cool is that !
With the search command this capability is referred to as the "IN operator". With the eval and where commands, it is implemented as the "IN function".
To use IN with the eval and where commands, you must use IN as an eval function. The Splunk documentation calls it the "in function".
And the syntax and usage are slightly different than with the search command.
Here are the supported syntax options:
...| eval new_field=if(IN(field,"value1","value2", ...), "value_if_true","value_if_false")
...| where field IN("value1","value2", ...)
...| where IN(field,"value1","value2", ...)
Note: The IN function, unlike the IN operator, can be specified in upper or lowercase. The IN function is shown in this blog in uppercase in the syntax and examples for clarity.
Let's start with the where command because it is fairly straight-forward.
The following example uses the where command to return IN=TRUE if one of the values in the status field matches one of the values in the list. The values in the status field are HTTP status codes. Because the codes are string values (not numeric values), you must enclose each value in quotation marks.
... | where status IN("400", "401", "403", "404", "406")
You could also specify this example as:
... | where IN(status,"400", "401", "403", "404", "406")
Using the IN function with the eval command is different than using IN with the where command. The eval command cannot accept Boolean values, you must use the IN function inside another function that can process the Boolean values returned by the IN function.
Let's go through an example where you can use the IN function as the first parameter for the IF function. We'll use the access.log file that is included with the Search Tutorial data.
In the following example, the IN function is used with the IF function to evaluate the action field. Then the stats command performs a calculation.
sourcetype=access_combined_wcookie
| eval activity=if(IN(action, "addtocart","purchase"),"Purchase Related","Other")
| stats count by activity
Here is what this search is doing:
The results appear on the Statistics tab and show the counts for how many events have Purchase Related activity and how many have Other types of activity.
This results table is great. You can also show the results in a chart. Switch to the Visualization tab and change the chart type to Pie Chart.
You can save this search as a dashboard panel or a report.
See the following Splunk documentation for more information:
The Splunk platform removes the barriers between data and action, empowering observability, IT and security teams to ensure their organizations are secure, resilient and innovative.
Founded in 2003, Splunk is a global company — with over 7,500 employees, Splunkers have received over 1,020 patents to date and availability in 21 regions around the world — and offers an open, extensible data platform that supports shared data across any environment so that all teams in an organization can get end-to-end visibility, with context, for every interaction and business process. Build a strong data foundation with Splunk.