Splunk's Search Processing Language (SPL) is behind every search in Splunk. It combines commands, functions, and operators that allow you to filter, transform, and analyze data from your ingested logs. In essence, SPL lets you search through massive amounts of data, apply filters to narrow down results, and format the output. Let's see how to use SPL.
Free Text Search
The simplest way to use SPL is to use free-text search, such as in the query below:
index=windowslogs alice
The query will search for all events containing the alice keyword (case-insensitive). If you don't know the field names or just want to run a quick hunt for a unique keyword, free-text searches are your best choice. However, to run more complex searches, you'd need to use search operators and work with parsed fields and their values.
Search Operators
Splunk operators are the building blocks used to construct any search query. These operators are used to filter, remove, and refine your search results based on the specified criteria. Below, we will cover relational, logical, and wildcard operators. Note that for the filters to work, the events must be parsed into fields - the next rooms will explain the process in more detail.
Relational Operators
These operators are used to compare two expressions. They determine the relationship between the expressions, such as whether one is equal to, not equal to, greater than, or less than the other. Let's check out some examples below.
| Operator |
Example |
Explanation |
Equals = |
UserName=Mark |
Search for all events in which the field name UserName is equal to Mark |
Not Equal To != |
UserName!=Mark |
Search for all events in which the field name UserName is not equal to Mark |
Less Than < |
Age<10 |
The field Age has a value of less than 10 |
Less Than or Equal To <= |
Age<=10 |
The field Age has a value of less than or equal to 10 |
Greater Than > |
Outbound_Traffic>50 |
The Outbound_Traffic field value is greater than 50 |
Greater Than or Equal To >= |
Outbound_Traffic>=50 |
The Outbound_Traffic field value is greater than or equal to 50 |
Let's get hands-on and use the != relational operator to locate all event logs in our index where the field AccountName is not equal to System. Start with the query below and don't forget to set your time range back to All time!
index=windowslogs AccountName!=SYSTEM
In the screenshot below and in your Splunk instance, you can see that we have successfully filtered for all events that do not include the AccountName field value of SYSTEM.
Logical Operators
Splunk supports the following logical operators, which can be used to connect or modify conditions and operate on Boolean values (true/false).
| Operator |
Example |
Explanation |
NOT |
NOT UserName=* |
Returns events where UserName field does not exist. Don't confuse it with != operator |
AND |
UserName=David AND IPAddress=10.10.10.10 |
Returns all events in which the UserName field is equal to David and the IPAddress field is equal to10.10.10.10 |
OR |
UserName=David OR UserName=John |
Returns all events in which the UserName field is equal to David or John |
IN |
UserName IN(David, John) |
A more convenient alternative to the OR keyword, especially for long lists. |
Let's get some more practice by appending the previous query to search for events that have the AccountName field James. With this query, you are telling Splunk to filter out the SYSTEM account name, and from the results, only see events from the account name James:
// AND operator is implied, so both queries are valid
index=windowslogs AccountName!=SYSTEM AND AccountName=James
index=windowslogs AccountName!=SYSTEM AccountName=James
Wildcards and CIDR Search
Splunk supports the use of wildcards and CIDR search for IP addresses to search ffor a partial or IP subnet match. For example:
| Symbol |
Example |
Explanation |
* |
status=*fail* |
This will return all events that have status field set to failed, failure, appfail, etc. |
* |
DestinationIp=172.* |
This will return all events that contain values like DestinationIp=172.90.0.0.1 or DestinationIp=172.18.5.22 |
N/A |
DestinationIp=172.18.0.0/16 |
This will return all events where DestinationIp field is within the 172.18.0.0/16 subnet |
Order of Evaluation
Quotes
In Splunk, quotation marks "" are used to define exact phrases or strings. You can wrap text in quotes, and Splunk will treat it as a single value. Quotes can also be used to escape search operators. For example:
index=windowslogs failed login: Search for events with failed and login keywords, in any order
index=windowslogs "failed login": Search for the exact phrase "failed login", word order matters
index=windowslogs "TO BE OR NOT TO BE": Search for the exact phrase containing NOT and OR
Parentheses
You can utilize parentheses in Splunk to help group conditions together and control how the search is applied. Since OR operator takes precedence over AND, parentheses can help set the correct order of conditions. For example, imagine you want to search for events containing alice and bob together, or charlie alone:
| Your Search |
How Splunk Evaluates It |
index=windowslogs alice AND bob OR charlie
(Implicit search, no parentheses) |
index=windowslogs alice AND (bob OR charlie)
(Mistake! Splunk evaluated OR before AND) |
index=windowslogs (alice AND bob) OR charlie
(Explicit search with parentheses) |
index=windowslogs (alice AND bob) OR charlie
(Correct! The results match your requirements) |