SPL Cheat Sheet: Essential Splunk Search Commands
SPL (Search Processing Language) is the query language of Splunk. A search retrieves events, then pipes (|) them through commands that filter, transform and format the results. This cheat sheet covers the commands you'll use most, and the ones Splunk's Core certification exams test most often.
index=web sourcetype=access_combined status>=500 earliest=-24h
| stats count AS errors, dc(clientip) AS clients BY host
| where errors > 10
| sort - errorsThis example finds server errors from the last 24 hours, counts them per host, keeps hosts with more than 10 errors, and sorts the worst first. Most real searches follow the same pattern: retrieve, filter, transform, format.
Search basics
Every search starts by retrieving events. The more specific this first part is, the faster the search.
| Command | Example | What it does |
|---|---|---|
| index / sourcetype | index=web sourcetype=access_combined | Limit the search to specific data. Always specify an index. |
| field=value | status=404 host=web01 | Match events where a field has a value. Field values are not case-sensitive here; field names are. |
| Wildcards | host=web* uri="/api/*" | Match partial values. Avoid leading wildcards (*error), which are slow. |
| AND / OR / NOT | status=500 OR status=503 NOT host=test* | Boolean operators must be uppercase. AND is implied between terms. |
| Comparison | bytes>10000 status!=200 | Numeric comparisons on fields. != only matches events where the field exists. |
| Quoted phrases | "failed password" | Match an exact phrase in the raw event. |
Time modifiers
Set time in the search itself to override the time picker. @ snaps to the start of a unit.
| Command | Example | What it does |
|---|---|---|
| Relative | earliest=-24h latest=now | The last 24 hours. |
| Snap to unit | earliest=-7d@d latest=@d | The last 7 complete days, from midnight to midnight. |
| Today so far | earliest=@d | From midnight today until now. |
| bin | | bin _time span=5m | Group timestamps into buckets (bucket is an alias). |
Filtering results
Narrow results after retrieval. Filter as early in the pipeline as you can.
| Command | Example | What it does |
|---|---|---|
| search | | search action=blocked | Filter with search syntax (wildcards, field=value) later in the pipeline. |
| where | | where bytes > 2*avg_bytes AND like(uri, "/admin%") | Filter with eval expressions: compare fields to each other, use functions. String literals need double quotes. |
| dedup | | dedup user | Keep only the first event for each value. With default ordering, that is the most recent. |
| head / tail | | head 20 | First or last N results (default 10). |
| regex | | regex src_ip="^10\." | Keep events where a field matches a regular expression. |
Fields and formatting
Shape results into exactly the columns you need.
| Command | Example | What it does |
|---|---|---|
| table | | table _time user action src_ip | Show only these fields, in this order. |
| fields | | fields - _raw | Keep (+) or remove (-) fields. Removing fields early improves performance. |
| rename | | rename src_ip AS "Source IP" | Rename fields. Quote names that contain spaces. |
| sort | | sort - count | Sort descending with -, ascending with +. Returns up to 10,000 results by default; use sort 0 for all. |
| eval | | eval mb=round(bytes/1024/1024, 2) | Create or overwrite a field with an expression. |
| rex | | rex field=_raw "user=(?<user>\w+)" | Extract a field at search time with a named regex group. |
| fillnull | | fillnull value=0 errors | Replace empty values. |
| spath | | spath | Extract fields from JSON or XML events. |
Statistics and reporting
Transforming commands turn events into tables, which you can visualize as charts.
| Command | Example | What it does |
|---|---|---|
| stats | | stats count, dc(user) AS users, avg(bytes) BY host | Aggregate across events. Common functions: count, dc, sum, avg, min, max, values, list, earliest, latest. |
| top / rare | | top limit=5 uri | Most or least common values, with count and percent (default 10). |
| chart | | chart count OVER host BY status | Two-dimensional table: rows from OVER, columns from BY. |
| timechart | | timechart span=1h count BY sourcetype | Statistics over time; _time is always the x-axis. Shows the top 10 series by default, the rest as OTHER. |
| eventstats | | eventstats avg(bytes) AS avg_bytes BY host | Add aggregate results to every event without collapsing them. |
| streamstats | | streamstats count AS attempt BY user | Running or cumulative statistics, in event order. |
| transaction | | transaction session_id maxspan=30m | Group related events into one. Flexible but slow; prefer stats when it can do the job. |
Useful eval functions
Use these inside eval and where.
| Command | Example | What it does |
|---|---|---|
| if | eval result=if(status>=400, "error", "ok") | Two-way condition. |
| case | eval level=case(status>=500, "server", status>=400, "client", true(), "ok") | Multi-way condition. true() is the catch-all. |
| coalesce | eval user=coalesce(user, username, "unknown") | First non-null value. |
| isnull / isnotnull | where isnull(email) | Test whether a field has a value. |
| like / match | where match(uri, "^/api/v[0-9]+/") | SQL-style (%) or regex pattern matching. |
| strftime | eval day=strftime(_time, "%Y-%m-%d") | Format a timestamp as text. |
| strptime | eval t=strptime(login, "%Y-%m-%d %H:%M:%S") | Parse text into a timestamp. |
| relative_time | where _time > relative_time(now(), "-1d@d") | Calculate a time offset from a timestamp. |
| lower / upper / len | eval u=lower(user) | String helpers. |
| mvcount / mvindex | eval n=mvcount(recipients) | Work with multivalue fields. |
Lookups and combining data
Enrich events with external data or combine searches.
| Command | Example | What it does |
|---|---|---|
| lookup | | lookup users.csv user OUTPUT department | Add fields from a lookup table to matching events. |
| inputlookup | | inputlookup users.csv | Read a lookup table directly. Must start the search, after a leading pipe. |
| outputlookup | | outputlookup blocked_ips.csv | Write results to a lookup table. |
| Subsearch | index=web [search index=threat | fields src_ip] | The inner search runs first and its results become filters for the outer search. Subsearches have result and time limits. |
| append | | append [search index=db error] | Add another search’s results to the end. |
| join | | join user [search index=hr] | Combine on a common field. Often slow; stats BY on a shared field is usually better. |
Performance tips
- Always specify index= (and sourcetype= when you can). This is the single biggest performance win.
- Filter before you transform. Put field=value terms in the base search, not in a later | search or | where.
- Use fields to drop what you don’t need early, especially _raw in large searches.
- Prefer stats over transaction and join whenever stats can produce the same answer.
- Avoid leading wildcards such as *error. They force Splunk to scan far more data.
- For accelerated data models, | tstats count from datamodel=Web by Web.status is dramatically faster than a raw search.
Common exam gotchas
Boolean order
In the search command, Splunk evaluates NOT first, then OR, then AND (with parentheses taking precedence over all three). Use parentheses to be explicit. The where command follows normal precedence.
NOT field=value vs field!=value
NOT status=200 also returns events with no status field. status!=200 returns only events that have a status field with a different value.
Case sensitivity
Field names are case-sensitive. Field values in the search command are not, but string comparisons in where and eval are.
where vs search
search uses field=value syntax with wildcards. where uses eval expressions: string literals need double quotes, and single quotes refer to field names.
stats vs eventstats vs streamstats
stats collapses events into a summary table. eventstats adds the summary to every event. streamstats adds a running calculation in event order.
top and timechart limits
top returns 10 values by default. A timechart split BY a field shows 10 series by default, with the remainder grouped as OTHER.
Practice these commands
Reading a cheat sheet only gets you so far. Splunky's free courses include SPL terminal exercises where you write these searches yourself:
- SPLK-1001 Core Certified User course: search basics, fields, stats, lookups and reports
- SPLK-1002 Power User course: subsearches, eventstats, streamstats, data models and macros
- Quick-fire drill: rapid practice to build recall
Planning your next exam? See SPLK-1001 vs SPLK-1002 and the Splunk certification path.