This topic explains how to refine data using filtering commands to narrow down relevant events and formatting commands to present results in an organized and insightful way. As a beginner, it's important to understand how each command works and how they fit into Splunk's Search Processing Language (SPL).
These two steps ensure that the data presented is concise, meaningful, and easy to interpret.
Filtering commands are used to specify the data you want to include or exclude in your search results. Here are the most commonly used filtering commands:
search CommandThe search command is the most basic and frequently used command for filtering events based on keywords or conditions.
search <keyword_or_condition>
index=web_logs is equivalent to search index=web_logs).AND, OR, and NOT for complex conditions.Filter by Keyword:
index=web_logs search error
Result: Returns all events containing the word "error".
Filter by Multiple Keywords:
index=errors search "404" OR "500"
Result: Retrieves logs containing either "404" or "500".
Exclude Events:
index=web_logs search NOT "debug"
Result: Returns all events except those containing the word "debug".
where CommandThe where command applies logical conditions to filter events based on specific field values or calculations.
where <condition>
>, <, =, !=, LIKE).search.Filter by Field Value:
index=orders | where quantity > 10
Result: Retrieves orders where the quantity field is greater than 10.
Filter by Text Matching:
index=users | where username LIKE "admin%"
Result: Returns events where the username starts with "admin".
Filter by Multiple Conditions:
index=products | where price > 100 AND category="electronics"
Result: Filters products where the price is greater than 100 and the category is "electronics".
fields CommandThe fields command controls which fields are included or excluded in the search results. This is particularly useful for simplifying output by focusing on relevant fields.
fields [+|-] <field1>, <field2>, ...
+ to include specific fields (default behavior if omitted).- to exclude specific fields.Include Fields:
index=employees | fields name, department
Result: Displays only the name and department fields in the output.
Exclude Fields:
index=employees | fields - salary
Result: Excludes the salary field from the output.
Combine with Other Filters:
index=orders | where quantity > 5 | fields order_id, product_name
Result: Filters orders with a quantity greater than 5 and displays only order_id and product_name.
Formatting commands refine the presentation of results. They are often used in combination with filtering commands to make data more readable.
eval CommandThe eval command is used to create new fields, modify existing ones, or perform calculations.
eval <new_field> = <expression>
Create a Calculated Field:
eval total_price = price * quantity
Result: Adds a new field total_price to each event.
String Concatenation:
eval full_name = first_name . " " . last_name
Result: Combines first_name and last_name into a new field full_name.
Conditional Field Creation:
eval status = if(price > 100, "High", "Low")
Result: Creates a status field with values "High" or "Low" based on the price.
table CommandThe table command displays results in a tabular format with only the specified fields.
table <field1>, <field2>, ...
Tabular Display of Fields:
index=orders | table order_id, customer_name, total_price
Result: Displays a table with order_id, customer_name, and total_price.
Combine with Calculated Fields:
index=products | eval profit = revenue - cost | table product_name, profit
Result: Displays a table with product_name and the calculated profit.
rename CommandThe rename command changes field names in the search results, making them more intuitive.
rename <old_field> AS <new_field>
Rename a Single Field:
index=sales | rename customer_id AS CustomerID
Result: Replaces customer_id with CustomerID in the output.
Rename Multiple Fields:
index=employees | rename emp_id AS EmployeeID, dept AS Department
Result: Renames emp_id to EmployeeID and dept to Department.
Filter Early in the Query:
Use search or where at the beginning of your query to reduce the dataset size.
Example:
index=web_logs search "error" | stats count BY status_code
Use eval for Dynamic Fields:
Create calculated fields to highlight key metrics.
Example:
eval profit_margin = (revenue - cost) / revenue
Focus the Output:
Use fields and table to display only the most relevant data.
Example:
index=orders | table order_id, customer_name, total_price
Wildcards allow you to match partial strings or patterns in your filters. They are particularly useful for filtering text fields.
Partial Match:
index=web_logs search url="*.html"
Result: Retrieves events where the url ends with .html.
Multiple Wildcards:
index=web_logs search host="web*server*"
Result: Matches host values like web1serverA or web2serverB.
You can use range operators to filter numeric fields effectively.
Greater Than or Equal To:
index=sales | where price >= 100
Result: Returns events where price is at least 100.
Between Two Values:
index=orders | where quantity > 5 AND quantity <= 20
Result: Retrieves events with quantity between 6 and 20.
Combining logical operators such as AND, OR, and NOT allows you to create complex filters.
Combine Conditions:
index=web_logs | where (status_code=404 AND method="GET") OR method="POST"
Result: Filters events where:
status_code is 404 and method is GET, ormethod is POST.Exclude Specific Events:
index=errors | search NOT ("debug" OR "info")
Result: Excludes events containing "debug" or "info".
When a field contains multiple values, you can filter based on one or more of these values.
Match Any Value:
index=products | where "electronics" IN category
Result: Retrieves events where category includes "electronics".
Use mvexpand for Granular Filtering:
index=products | mvexpand tags | search tags="popular"
Result: Expands multivalue fields and filters events with "popular" as one of the tags.
evalYou can use conditional logic to format results dynamically.
Categorize Data:
eval price_category = if(price > 100, "High", "Low")
Result: Adds a price_category field with values "High" or "Low" based on the price.
Flagging Suspicious Events:
eval alert = if(status_code=500, "Critical", "Normal")
Result: Adds an alert field to label events as "Critical" or "Normal".
tableThe table command can be used to reorder fields for better readability.
index=orders | table customer_name, order_id, total_price
Result: Displays results in the specified column order.
Combining rename, eval, and table helps create clear and concise output.
index=employees | eval FullName=first_name." ".last_name | rename dept AS Department | table FullName, Department, salary
Result: Displays a table with renamed and dynamically created fields.
Cause: Filters may be too restrictive or field names are incorrect.
Solution:
Example (Incorrect):
index=orders | where price > 1000 AND quantity > 50
Corrected:
index=orders | where price > 1000 OR quantity > 50
Cause: Some fields may not exist in all events.
Solution:
Use coalesce to handle null values.
Example:
eval product_name = coalesce(product_name, "Unknown")
fields to exclude unnecessary data.Here are some hands-on exercises to test your understanding of filtering and formatting results:
Retrieve all events with status code 200:
index=web_logs | search status_code=200
Find all orders with a quantity greater than 10 but less than 50:
index=orders | where quantity > 10 AND quantity < 50
evalAdd a new field total_price by multiplying price and quantity:
index=sales | eval total_price = price * quantity | table product, total_price
Categorize events based on response time:
index=web_logs | eval response_category = if(response_time > 500, "Slow", "Fast") | table response_time, response_category
Filter First, Format Later
search and where before applying formatting commands like eval or table.Use Meaningful Names
Simplify Complex Queries
|).search and whereIn Splunk, both search and where are used to filter events, but they function at different stages of the search pipeline and serve different purposes.
search CommandPerforms keyword-based filtering during the indexing phase (before events are fully loaded).
More efficient when you're filtering by simple terms or indexed fields.
Example:
index=web_logs status=404
status equals 404 at the earliest stage possible.where CommandApplies post-index filtering after events are returned from disk.
Ideal for conditional logic, numerical comparisons, or complex field evaluations.
Example:
index=web_logs | where status >= 400 AND status < 500
search is filtering during the indexing stage and is best suited for keywords or indexed fields. where is applied after events are loaded and is suitable for precise field-based conditions.
This distinction is occasionally tested in Splunk certification exams.
table and fieldsBoth table and fields control which fields are displayed in search results, but they have very different behaviors.
fields CommandLimits field visibility while preserving the original event structure.
Good for reducing clutter and improving performance, especially in exploratory searches.
Example:
index=orders | fields user_id, amount
user_id and amount fields in the UI.table CommandRestructures results entirely into a new table format with only the specified fields.
Removes all original event metadata and raw content.
Ideal for creating clean reports or dashboard tables.
Example:
index=orders | table user_id, amount
user_id and amount columns.fields preserves the raw event and just filters which fields are visible. table rebuilds the result set into a clean, tabular format and is best for final reporting or visual display.
mvexpandThe mvexpand command is used to expand multivalue fields so that each value appears in a separate row.
mvexpand must be placed before the field is used in any transformation command.
stats, table, or eval before mvexpand, the field may not expand properly.index=logs | mvexpand error_codes | stats count BY error_codes
error_codes is treated as a separate event.index=logs | stats count BY error_codes | mvexpand error_codes
mvexpand has no effect.Use search for indexed filters and keywords; use where for complex field conditions.
Choose fields to limit visible fields without altering structure; use table to present formatted output for reporting.
Always place mvexpand before any command that consumes the field.
Why might a Splunk user prefer where over search when filtering numerical conditions?
Because where evaluates numerical comparisons correctly.
The where command processes expressions using evaluation logic, allowing proper numerical comparisons such as greater-than, less-than, and arithmetic operations. In contrast, search primarily performs textual matching and Boolean filtering. When filtering numeric fields such as response times or status codes, where ensures that comparisons are interpreted numerically rather than lexically. For example, where latency > 100 correctly evaluates numeric values, whereas search latency>100 may behave inconsistently depending on field formatting. Users frequently encounter unexpected results when attempting numeric comparisons using search. Using where prevents these issues by explicitly evaluating the expression as a conditional calculation.
Demand Score: 73
Exam Relevance Score: 84
What problem does the fillnull command solve when preparing results for visualization?
It replaces null field values with a specified default value.
In many Splunk searches, certain events may not contain all expected fields. When aggregating results or building visualizations, these missing values appear as null and can cause incomplete tables or inconsistent charts. The fillnull command replaces null values with a defined value such as 0 or "unknown". This ensures that all records contain consistent field values and prevents missing data from affecting statistical calculations. For example, replacing null numeric fields with 0 allows accurate aggregation using functions like sum or avg. A common misunderstanding is expecting fillnull to create fields that never appear in the dataset; it only replaces null values in fields that exist in the results.
Demand Score: 71
Exam Relevance Score: 82
Why might a Splunk user apply the eval command before filtering results with where?
Because eval creates or modifies fields that can then be evaluated by where.
The eval command allows users to calculate new fields or modify existing ones using expressions. These calculated fields can then be used in conditional filtering with the where command. For example, a user might compute latency = end_time - start_time using eval, and then filter events using where latency > 200. Without the eval step, the field required for the comparison would not exist. This pattern is common when deriving metrics from raw fields before applying conditional logic. A frequent mistake is attempting to reference fields in where that have not yet been created or calculated earlier in the search pipeline.
Demand Score: 72
Exam Relevance Score: 85
What is the key functional difference between the search command and the where command in Splunk?
search filters events using simple field-value matching, while where filters events using evaluation expressions.
The search command is designed for basic filtering such as matching field values, keywords, or Boolean conditions. It is optimized for early filtering in the search pipeline and works well with indexed fields. The where command, however, evaluates expressions using comparison operators and functions similar to SQL conditions. This allows more complex filtering logic, such as mathematical comparisons or calculated expressions. For example, where response_time > 500 performs numerical evaluation, which search cannot reliably perform. A common mistake is trying to perform numerical comparisons inside the search command, which can produce unexpected results because search treats values primarily as strings rather than evaluated expressions.
Demand Score: 76
Exam Relevance Score: 88