Dashboards & Visualizations

how to make loop in splunk query

carinahOliveira
Explorer

I have a query that returns multiple times and I need to step through this result doing a subtraction count between one and the other.

Example:
Time = time[0] - time[1]
Time = time[1] - time[2]

etc ....


My search looks like this:

 

<my search>
| eval Time=_time
| stats values(Time) as Time by UserName| eval reconnection=if(UserName == UserName, tonumber(mvindex(Time,1))-tonumber(mvindex(Time,0)), "falha")
| where reconnection>0 AND reconnection<600
| eval reconnection=tostring(reconnection, "duration")

1 Solution

carinahOliveira
Explorer

I managed to solve it and I'll leave the solution here in case anyone needs it.

As mentioned earlier, there is no way to create a loop, but we can create a range and use it to iterate

Here's how I did it:

---------------------
| eval i = mvrange(0,10)
| mvexpand i

----------------------

 

 

<my search>

| eval Time=_time
| stats values(Time) as Time by UserName

| eval i = mvrange(0,10)
| mvexpand i

| eval reconnection=if(UserName==UserName, tonumber(mvindex(Time,i+1))-tonumber(mvindex(Time,i)), "falha")
| where reconnection>0 AND reconnection<600

| eval reconnection=tostring(reconnection, "duration")

View solution in original post

0 Karma

carinahOliveira
Explorer

I managed to solve it and I'll leave the solution here in case anyone needs it.

As mentioned earlier, there is no way to create a loop, but we can create a range and use it to iterate

Here's how I did it:

---------------------
| eval i = mvrange(0,10)
| mvexpand i

----------------------

 

 

<my search>

| eval Time=_time
| stats values(Time) as Time by UserName

| eval i = mvrange(0,10)
| mvexpand i

| eval reconnection=if(UserName==UserName, tonumber(mvindex(Time,i+1))-tonumber(mvindex(Time,i)), "falha")
| where reconnection>0 AND reconnection<600

| eval reconnection=tostring(reconnection, "duration")

0 Karma

richgalloway
SplunkTrust
SplunkTrust

Splunk doesn't have loops like programming languages.  The foreach and map commands let you iterate over fields or events, respectively, but those don't do what you describe.

The closest you can get is the streamstats command with the range function.  This command computes the difference between the current value of the 'time' field and the previous one.

| streamstats window=1 range(time) as Time

 

---
If this reply helps you, Karma would be appreciated.
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas

Why Splunk Customers Should Attend Cisco Live 2026 Las Vegas     Cisco Live 2026 is almost here, and this ...

What Is the Name of the USB Key Inserted by Bob Smith? (BOTS Hint, Not the Answer)

Hello Splunkers,   So you searched, “what is the name of the usb key inserted by bob smith?”  Not gonna lie… ...

Automating Threat Operations and Threat Hunting with Recorded Future

    Automating Threat Operations and Threat Hunting with Recorded Future June 29, 2026 | Register   Is your ...