<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: foreach command in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/foreach-command/m-p/327650#M162627</link>
    <description>&lt;P&gt;Hi, Kristof!&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;foreach&lt;/CODE&gt; 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 &lt;CODE&gt;bytes in&lt;/CODE&gt; and &lt;CODE&gt;bytes out&lt;/CODE&gt; to kB, you could write smth like that:&lt;BR /&gt;
&lt;PRE&gt;&lt;/PRE&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| foreach bytes*
  [ eval &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;_kB = round('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;' / 1024) ]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;/P&gt;

&lt;P&gt;In your case &lt;CODE&gt;foreach&lt;/CODE&gt; command is not so necessary. But you can use it in this way:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| foreach "bytes out"
  [ eval &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; = if(like(server, "www%"), round('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;' / 1024), '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') ]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 02 Mar 2018 14:16:37 GMT</pubDate>
    <dc:creator>nryabykh</dc:creator>
    <dc:date>2018-03-02T14:16:37Z</dc:date>
    <item>
      <title>foreach command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/foreach-command/m-p/327649#M162626</link>
      <description>&lt;P&gt;Hi everyone, &lt;/P&gt;

&lt;P&gt;I'm trying to get my head around this foreach statement but no luck so far ... &lt;/P&gt;

&lt;P&gt;Foreach seems like the most obscure command ever .... &lt;/P&gt;

&lt;P&gt;foreach * means everything ... but everything in what?&lt;/P&gt;

&lt;P&gt;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?&lt;/P&gt;

&lt;P&gt;Say you have a simple dataset as a result:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;                   bytes in               bytes out
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;www1                1200                       3000&lt;BR /&gt;
www2                4500                         3600&lt;BR /&gt;&lt;BR /&gt;
db1               4900                        5000&lt;/P&gt;

&lt;P&gt;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. &lt;/P&gt;

&lt;P&gt;How can I match that with this foreach?&lt;/P&gt;

&lt;P&gt;Thanks&lt;BR /&gt;
Kristof&lt;/P&gt;</description>
      <pubDate>Fri, 02 Mar 2018 10:15:15 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/foreach-command/m-p/327649#M162626</guid>
      <dc:creator>kristofvdbdavin</dc:creator>
      <dc:date>2018-03-02T10:15:15Z</dc:date>
    </item>
    <item>
      <title>Re: foreach command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/foreach-command/m-p/327650#M162627</link>
      <description>&lt;P&gt;Hi, Kristof!&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;foreach&lt;/CODE&gt; 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 &lt;CODE&gt;bytes in&lt;/CODE&gt; and &lt;CODE&gt;bytes out&lt;/CODE&gt; to kB, you could write smth like that:&lt;BR /&gt;
&lt;PRE&gt;&lt;/PRE&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| foreach bytes*
  [ eval &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;_kB = round('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;' / 1024) ]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;/P&gt;

&lt;P&gt;In your case &lt;CODE&gt;foreach&lt;/CODE&gt; command is not so necessary. But you can use it in this way:&lt;BR /&gt;
&lt;PRE&gt;&lt;CODE&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| foreach "bytes out"
  [ eval &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; = if(like(server, "www%"), round('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;' / 1024), '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;') ]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Mar 2018 14:16:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/foreach-command/m-p/327650#M162627</guid>
      <dc:creator>nryabykh</dc:creator>
      <dc:date>2018-03-02T14:16:37Z</dc:date>
    </item>
    <item>
      <title>Re: foreach command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/foreach-command/m-p/327651#M162628</link>
      <description>&lt;P&gt;you have to declare this&lt;/P&gt;

&lt;P&gt;| foreach [eval totalinKB=0.001*&amp;lt;&amp;gt;]]&lt;/P&gt;

&lt;P&gt;Please response if you are unclear&lt;/P&gt;</description>
      <pubDate>Fri, 02 Mar 2018 14:28:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/foreach-command/m-p/327651#M162628</guid>
      <dc:creator>logloganathan</dc:creator>
      <dc:date>2018-03-02T14:28:36Z</dc:date>
    </item>
    <item>
      <title>Re: foreach command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/foreach-command/m-p/327652#M162629</link>
      <description>&lt;P&gt;Hi guys ... &lt;/P&gt;

&lt;P&gt;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?&lt;BR /&gt;
What happens if &amp;lt;&amp;gt; contains non numeric values?&lt;/P&gt;

&lt;P&gt;Still confused ... &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 02 Mar 2018 14:34:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/foreach-command/m-p/327652#M162629</guid>
      <dc:creator>kristofvdbdavin</dc:creator>
      <dc:date>2018-03-02T14:34:14Z</dc:date>
    </item>
    <item>
      <title>Re: foreach command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/foreach-command/m-p/327653#M162630</link>
      <description>&lt;P&gt;The &lt;CODE&gt;foreach&lt;/CODE&gt; command works on specified columns of every rows in the search result. So, &lt;CODE&gt;| foreach * [&lt;/CODE&gt;, 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. &lt;CODE&gt;| foreach "bytes out" "bytes in" "other field" [...&lt;/CODE&gt;) or wildcarded names to match all columns names following that pattern (e.g. &lt;CODE&gt;| foreach "bytes*" [...&lt;/CODE&gt;) . 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 &lt;CODE&gt;isnum&lt;/CODE&gt; function, before manipulation).&lt;/P&gt;</description>
      <pubDate>Fri, 02 Mar 2018 19:43:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/foreach-command/m-p/327653#M162630</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2018-03-02T19:43:58Z</dc:date>
    </item>
    <item>
      <title>Re: foreach command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/foreach-command/m-p/327654#M162631</link>
      <description>&lt;P&gt;Thanks for the detailed explanation!&lt;/P&gt;</description>
      <pubDate>Fri, 02 Mar 2018 20:00:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/foreach-command/m-p/327654#M162631</guid>
      <dc:creator>kristofvdbdavin</dc:creator>
      <dc:date>2018-03-02T20:00:02Z</dc:date>
    </item>
    <item>
      <title>Re: foreach command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/foreach-command/m-p/327655#M162632</link>
      <description>&lt;P&gt;Technically, one KB is 1024 bytes, not 1000 bytes.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 10:11:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/foreach-command/m-p/327655#M162632</guid>
      <dc:creator>simonzfor</dc:creator>
      <dc:date>2019-10-25T10:11:09Z</dc:date>
    </item>
    <item>
      <title>Re: foreach command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/foreach-command/m-p/327656#M162633</link>
      <description>&lt;P&gt;This seems like it should be an edit, a comment or a new question rather than an answer.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Oct 2019 10:12:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/foreach-command/m-p/327656#M162633</guid>
      <dc:creator>simonzfor</dc:creator>
      <dc:date>2019-10-25T10:12:01Z</dc:date>
    </item>
  </channel>
</rss>

