<?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: How to combine foreach and fieldformat? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240440#M71462</link>
    <description>&lt;P&gt;With the above search? That shouldn't happen.&lt;/P&gt;</description>
    <pubDate>Thu, 19 Nov 2015 13:44:30 GMT</pubDate>
    <dc:creator>jeffland</dc:creator>
    <dc:date>2015-11-19T13:44:30Z</dc:date>
    <item>
      <title>How to combine foreach and fieldformat?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240434#M71456</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I've a timechart table for revenue grouped by product.&lt;/P&gt;

&lt;P&gt;_time | productA | product B | product C&lt;/P&gt;

&lt;P&gt;I would like to display all revenue values as 1,000 € instead of 1000.&lt;BR /&gt;
Now I could do it like this, but I would have to know all possible products.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;....
| fieldformat productA=tostring(productA, "commas")." €"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Is there a way to achieve this with the foreach command? I was not able to figure it out.&lt;/P&gt;

&lt;P&gt;Best&lt;BR /&gt;
Heinz&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Update:&lt;/STRONG&gt;&lt;BR /&gt;
This formats all revenue values, but here the time field is empty:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| foreach * [fieldformat  &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=tostring('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;', "commas")." €"]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;A possible workaround seems to be:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval time=strftime(_time,"%Y-%m-%d")
| addcoltotals labelfield=time label="TOTALS"
| addtotals fieldname="TOTALS"
| foreach * [fieldformat  &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=tostring('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;', "commas")." €"]
| foreach time [fieldformat  &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=tostring('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;')]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But it does not format the values every time I run the search. Sometimes it works, sometimes not...&lt;/P&gt;</description>
      <pubDate>Thu, 19 Nov 2015 09:10:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240434#M71456</guid>
      <dc:creator>HeinzWaescher</dc:creator>
      <dc:date>2015-11-19T09:10:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine foreach and fieldformat?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240435#M71457</link>
      <description>&lt;P&gt;I can't exactly reproduce your problem, when I run this search:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal | timechart count by group | foreach * [fieldformat  &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=tostring('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;', "commas")." €"]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I get an intact _time field and all other fields are displayed as desired. However, when I use a different name for my _time values such as your &lt;CODE&gt;time&lt;/CODE&gt;, then the foreach will also be applied to that field. Is there a reason you use &lt;CODE&gt;time&lt;/CODE&gt; instead of &lt;CODE&gt;_time&lt;/CODE&gt;?&lt;/P&gt;

&lt;P&gt;If you have to, you could prevent this by either using a letter that all your products have in common that is different from t, i.e. if your product columns are actually called &lt;CODE&gt;product_abc&lt;/CODE&gt; and &lt;CODE&gt;product_def&lt;/CODE&gt; you could use &lt;CODE&gt;foreach product_*&lt;/CODE&gt; and skip fieldformatting your field &lt;CODE&gt;time&lt;/CODE&gt; by that. You could also use an if inside your subsearch to check if the current field name "time" and not change it if it is, such as this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| foreach * [fieldformat  &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=if(&amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;!=time, tostring('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;', "commas")." €", '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;')]
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Sep 2020 07:56:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240435#M71457</guid>
      <dc:creator>jeffland</dc:creator>
      <dc:date>2020-09-29T07:56:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine foreach and fieldformat?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240436#M71458</link>
      <description>&lt;P&gt;The reason why I use time here, is because of the addcoltotals. I want to label the values as "TOTALS", but the _time fields does not accept the string.&lt;/P&gt;

&lt;P&gt;The if-approach works fine so far, I haven't experienced wrong formats yet. Thanks!&lt;/P&gt;

&lt;P&gt;Another question appeared, when I tried to send this dashboard as PDF. It seems that the PDF-Delivery doesn't like the format. When I preview this as PDF it looks fine, but the delivery per mail only shows one empty row in the PDF.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Nov 2015 13:16:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240436#M71458</guid>
      <dc:creator>HeinzWaescher</dc:creator>
      <dc:date>2015-11-19T13:16:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine foreach and fieldformat?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240437#M71459</link>
      <description>&lt;P&gt;I checked this without "€" and with "$" instead of it. Both times this works, so Splunk seems to have problem with the € sign here.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Nov 2015 13:26:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240437#M71459</guid>
      <dc:creator>HeinzWaescher</dc:creator>
      <dc:date>2015-11-19T13:26:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine foreach and fieldformat?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240438#M71460</link>
      <description>&lt;P&gt;I just tried, and I was able to add a "TOTALS" string to my _time field:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal | timechart count by group | table _time per_source_thruput pipeline queue | addcoltotals labelfield=_time label="TOTALS" | addtotals fieldname="TOTALS" | foreach * [fieldformat  &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=tostring('&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;', "commas")." €"]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Anyway, glad we found a solution for you. Unfortunately, I can't really help you with the PDF delivery errors, maybe you should to open a separate question for that.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Nov 2015 13:34:22 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240438#M71460</guid>
      <dc:creator>jeffland</dc:creator>
      <dc:date>2015-11-19T13:34:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine foreach and fieldformat?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240439#M71461</link>
      <description>&lt;P&gt;Hm, in my search it looks like this:&lt;/P&gt;

&lt;P&gt;_time&lt;BR /&gt;&lt;BR /&gt;
2015-11-16 00:00:00&lt;BR /&gt;&lt;BR /&gt;
2015-11-17 00:00:00&lt;BR /&gt;&lt;BR /&gt;
2015-11-18 00:00:00&lt;BR /&gt;&lt;BR /&gt;
0NaN-NaN-NaN NaN:NaN:NaN&lt;/P&gt;</description>
      <pubDate>Thu, 19 Nov 2015 13:38:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240439#M71461</guid>
      <dc:creator>HeinzWaescher</dc:creator>
      <dc:date>2015-11-19T13:38:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine foreach and fieldformat?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240440#M71462</link>
      <description>&lt;P&gt;With the above search? That shouldn't happen.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Nov 2015 13:44:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240440#M71462</guid>
      <dc:creator>jeffland</dc:creator>
      <dc:date>2015-11-19T13:44:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine foreach and fieldformat?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240441#M71463</link>
      <description>&lt;P&gt;Yes, just copy &amp;amp; paste&lt;BR /&gt;
 We are currently still on 6.2.2, perhaps this is a new feature? &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Nov 2015 13:45:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240441#M71463</guid>
      <dc:creator>HeinzWaescher</dc:creator>
      <dc:date>2015-11-19T13:45:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine foreach and fieldformat?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240442#M71464</link>
      <description>&lt;P&gt;"It's not a bug, it's a feature" &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Well this is strange. It works for me on 6.3.0 and on 6.2.3.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Nov 2015 13:59:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240442#M71464</guid>
      <dc:creator>jeffland</dc:creator>
      <dc:date>2015-11-19T13:59:09Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine foreach and fieldformat?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240443#M71465</link>
      <description>&lt;P&gt;I will check it with the next update.&lt;BR /&gt;
Can you tell my why we are using eval instead of fieldformat in the foreach command here?&lt;/P&gt;</description>
      <pubDate>Thu, 19 Nov 2015 14:00:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240443#M71465</guid>
      <dc:creator>HeinzWaescher</dc:creator>
      <dc:date>2015-11-19T14:00:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine foreach and fieldformat?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240444#M71466</link>
      <description>&lt;P&gt;Which search exactly? I don't see any eval. &lt;/P&gt;</description>
      <pubDate>Thu, 19 Nov 2015 15:20:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240444#M71466</guid>
      <dc:creator>jeffland</dc:creator>
      <dc:date>2015-11-19T15:20:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine foreach and fieldformat?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240445#M71467</link>
      <description>&lt;P&gt;I think I've mixed something up. I must have changed all "fieldformat" with eval automatically, because the fieldformat doesn't work after foreach on my side.&lt;/P&gt;

&lt;P&gt;When I use your example:&lt;/P&gt;

&lt;P&gt;index=_internal | timechart count by group | table _time per_source_thruput pipeline queue | addcoltotals labelfield=_time label="TOTALS" | addtotals fieldname="TOTALS" | foreach * [fieldformat  &amp;lt;&amp;gt;=tostring('&amp;lt;&amp;gt;', "commas")." €"]&lt;/P&gt;

&lt;P&gt;This does not change the values. But with eval instead fieldformat it does.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 07:56:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240445#M71467</guid>
      <dc:creator>HeinzWaescher</dc:creator>
      <dc:date>2020-09-29T07:56:13Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine foreach and fieldformat?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240446#M71468</link>
      <description>&lt;P&gt;Huh? That's strange... Time to take that to the IRC channel if you ask me &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Nov 2015 10:21:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240446#M71468</guid>
      <dc:creator>jeffland</dc:creator>
      <dc:date>2015-11-20T10:21:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to combine foreach and fieldformat?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240447#M71469</link>
      <description>&lt;P&gt;I will try to join&lt;/P&gt;</description>
      <pubDate>Fri, 20 Nov 2015 10:29:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-combine-foreach-and-fieldformat/m-p/240447#M71469</guid>
      <dc:creator>HeinzWaescher</dc:creator>
      <dc:date>2015-11-20T10:29:34Z</dc:date>
    </item>
  </channel>
</rss>

