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
SplunkTrust
SplunkTrust

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>>') ]

Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...