Splunk Search

foreach command

kristofvdbdavin
New Member

Hi everyone,

I'm trying to get my head around this foreach statement but no luck so far ...

Foreach seems like the most obscure command ever ....

foreach * means everything ... but everything in what?

foreach ... in the example on the docs ... they use new variables but I want to know how you can/are supposed to run through your dataset?

Say you have a simple dataset as a result:

                   bytes in               bytes out

www1 1200 3000
www2 4500 3600

db1 4900 5000

these are values in bytes and I want them converted in kBs but let's say I only want it to be done to the bytes out of every webserver (www*) and the bytes in need to stay in bytes.

How can I match that with this foreach?

Thanks
Kristof

Tags (1)
0 Karma
1 Solution

nryabykh
Path Finder

Hi, Kristof!

foreach is used when you need to apply the same command (of several commands) to multiple columns (fields). For example, if you need to transform both bytes in and bytes out to kB, you could write smth like that:

| foreach bytes*
  [ eval <<FIELD>>_kB = round('<<FIELD>>' / 1024) ]

In your case foreach command is not so necessary. But you can use it in this way:

| foreach "bytes out"
  [ eval <<FIELD>> = if(like(server, "www%"), round('<<FIELD>>' / 1024), '<<FIELD>>') ]

View solution in original post

somesoni2
Revered Legend

The foreach command works on specified columns of every rows in the search result. So, | foreach * [, will run the foreach expression (whatever you specify within square brackets) for each column in your search result. If you only want it to be applied for specific columns, you need to provide either names of those columns, either full names (e.g. | foreach "bytes out" "bytes in" "other field" [...) or wildcarded names to match all columns names following that pattern (e.g. | foreach "bytes*" [...) . If you think the values of field can be of different data type, your expression should handle them accordingly. (e.g. check if its' number using isnum function, before manipulation).

kristofvdbdavin
New Member

Thanks for the detailed explanation!

0 Karma

kristofvdbdavin
New Member

Hi guys ...

Thanks for the answers. So each value in your output will be replaced with 1/1024th of your value if you would convert to KB basically?
What happens if <> contains non numeric values?

Still confused ... 😞

0 Karma

simonzfor
Explorer

This seems like it should be an edit, a comment or a new question rather than an answer.

0 Karma

logloganathan
Motivator

you have to declare this

| foreach [eval totalinKB=0.001*<>]]

Please response if you are unclear

0 Karma

simonzfor
Explorer

Technically, one KB is 1024 bytes, not 1000 bytes.

0 Karma

nryabykh
Path Finder

Hi, Kristof!

foreach is used when you need to apply the same command (of several commands) to multiple columns (fields). For example, if you need to transform both bytes in and bytes out to kB, you could write smth like that:

| foreach bytes*
  [ eval <<FIELD>>_kB = round('<<FIELD>>' / 1024) ]

In your case foreach command is not so necessary. But you can use it in this way:

| foreach "bytes out"
  [ eval <<FIELD>> = if(like(server, "www%"), round('<<FIELD>>' / 1024), '<<FIELD>>') ]

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!

Where Innovation Takes Flight: The Splunk4Aviation Flight Sim Lands at .conf26

If you hear someone at .conf26 shouting "gear down, GEAR DOWN" across the show floor, you have found us.  The ...

Turn Cisco Telemetry Into Action with Cisco Data Fabric, powered by the Splunk ...

The surge in machine data is already hitting enterprise budgets, and the agentic era will only intensify it. ...

Persistent Queue at TcpOut — One of Splunk's Most Practical Features

Splunk introduced persistent queueing at the tcpout layer as one of the most practical resilience features in ...