<?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 How do I use an if-statement to reduce rows of a field in a Splunk result set? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-use-an-if-statement-to-reduce-rows-of-a-field-in-a/m-p/449502#M127264</link>
    <description>&lt;P&gt;I have the following Splunk base search: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=serverA FATAL OR ERROR OR WARN | rex field=_raw max_match=1 "(?&amp;lt;error_type&amp;gt;WARN|ERROR|FATAL)"  | rex field=_raw "] (?&amp;lt;error_location&amp;gt;.*?) " | rex field=_raw "\) (?&amp;lt;description&amp;gt;.*)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I then pipe this result to &lt;CODE&gt;| eventstats count by error_type&lt;/CODE&gt; to get individual counts of each error type. Finally, I also want to return a set of &lt;CODE&gt;description&lt;/CODE&gt; for each of the three error_types, whose number of results is reliant on the count for each error_type. For example, if the count of &lt;CODE&gt;WARN&lt;/CODE&gt; is greater than 5, then it would only grab the first 5 descriptions. If the count is less, it would show them all.&lt;/P&gt;

&lt;P&gt;I'm trying a syntax like this with no luck so far:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=GRServerPROD_B FATAL OR ERROR OR WARN | rex field=_raw max_match=1 "(?&amp;lt;error_type&amp;gt;WARN|ERROR|FATAL)"  | rex field=_raw "] (?&amp;lt;error_location&amp;gt;.*?) " | rex field=_raw "\) (?&amp;lt;description&amp;gt;.*)"| eventstats count by error_type | eval description_set=if(count&amp;gt;5, &amp;lt;return top 5 descriptions&amp;gt;, &amp;lt;return all descriptions&amp;gt;) | stats values(description_set) as descriptions, values(count) as count by error_type
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I also tried running sub-searches using &lt;CODE&gt;map&lt;/CODE&gt; but this seemed pretty lengthy.&lt;/P&gt;</description>
    <pubDate>Tue, 29 Sep 2020 21:11:20 GMT</pubDate>
    <dc:creator>samsam48</dc:creator>
    <dc:date>2020-09-29T21:11:20Z</dc:date>
    <item>
      <title>How do I use an if-statement to reduce rows of a field in a Splunk result set?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-use-an-if-statement-to-reduce-rows-of-a-field-in-a/m-p/449502#M127264</link>
      <description>&lt;P&gt;I have the following Splunk base search: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=serverA FATAL OR ERROR OR WARN | rex field=_raw max_match=1 "(?&amp;lt;error_type&amp;gt;WARN|ERROR|FATAL)"  | rex field=_raw "] (?&amp;lt;error_location&amp;gt;.*?) " | rex field=_raw "\) (?&amp;lt;description&amp;gt;.*)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I then pipe this result to &lt;CODE&gt;| eventstats count by error_type&lt;/CODE&gt; to get individual counts of each error type. Finally, I also want to return a set of &lt;CODE&gt;description&lt;/CODE&gt; for each of the three error_types, whose number of results is reliant on the count for each error_type. For example, if the count of &lt;CODE&gt;WARN&lt;/CODE&gt; is greater than 5, then it would only grab the first 5 descriptions. If the count is less, it would show them all.&lt;/P&gt;

&lt;P&gt;I'm trying a syntax like this with no luck so far:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=GRServerPROD_B FATAL OR ERROR OR WARN | rex field=_raw max_match=1 "(?&amp;lt;error_type&amp;gt;WARN|ERROR|FATAL)"  | rex field=_raw "] (?&amp;lt;error_location&amp;gt;.*?) " | rex field=_raw "\) (?&amp;lt;description&amp;gt;.*)"| eventstats count by error_type | eval description_set=if(count&amp;gt;5, &amp;lt;return top 5 descriptions&amp;gt;, &amp;lt;return all descriptions&amp;gt;) | stats values(description_set) as descriptions, values(count) as count by error_type
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I also tried running sub-searches using &lt;CODE&gt;map&lt;/CODE&gt; but this seemed pretty lengthy.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 21:11:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-use-an-if-statement-to-reduce-rows-of-a-field-in-a/m-p/449502#M127264</guid>
      <dc:creator>samsam48</dc:creator>
      <dc:date>2020-09-29T21:11:20Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use an if-statement to reduce rows of a field in a Splunk result set?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-use-an-if-statement-to-reduce-rows-of-a-field-in-a/m-p/449503#M127265</link>
      <description>&lt;P&gt;Hi  samsam48,&lt;BR /&gt;
I cannot test your search but I had a problem like your, so you have to try something like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; sourcetype=GRServerPROD_B FATAL OR ERROR OR WARN 
| rex field=_raw max_match=1 "(?&amp;lt;error_type&amp;gt;WARN|ERROR|FATAL)"  
| rex field=_raw "] (?&amp;lt;error_location&amp;gt;.*?) " 
| rex field=_raw "\) (?&amp;lt;description&amp;gt;.*)"
| stats values(description) AS description dc(description) AS different_descriptions count by error_type
| eval description=if(different_descriptions&amp;gt;5,"There are "+different_descriptions+" description",description)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If you want to have all the description on the same row, add at the end of your search &lt;CODE&gt;| nomv description&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;In addition I suggest to use index parameter to have more performant searches.&lt;/P&gt;

&lt;P&gt;Bye.&lt;BR /&gt;
Giuseppe&lt;/P&gt;</description>
      <pubDate>Wed, 05 Sep 2018 15:27:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-use-an-if-statement-to-reduce-rows-of-a-field-in-a/m-p/449503#M127265</guid>
      <dc:creator>gcusello</dc:creator>
      <dc:date>2018-09-05T15:27:16Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use an if-statement to reduce rows of a field in a Splunk result set?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-use-an-if-statement-to-reduce-rows-of-a-field-in-a/m-p/449504#M127266</link>
      <description>&lt;P&gt;Hi cusello,&lt;/P&gt;

&lt;P&gt;This query doesn't narrow down the results of &lt;CODE&gt;description&lt;/CODE&gt; though, if I'm not mistaken? If &lt;CODE&gt;count&lt;/CODE&gt; for one value of &lt;CODE&gt;error_type&lt;/CODE&gt; was over 5, then I'd only want 5 &lt;CODE&gt;descriptions&lt;/CODE&gt; from that given value of &lt;CODE&gt;error_type&lt;/CODE&gt;. For example, if &lt;CODE&gt;error_type&lt;/CODE&gt;= &lt;CODE&gt;WARN&lt;/CODE&gt;, and there were 1000 warnings, then I'd want the final chart to hold only 5 descriptions (it's too much to show all 1000 in an email). However, if &lt;CODE&gt;error_type&lt;/CODE&gt; =&lt;CODE&gt;FATAL&lt;/CODE&gt; and there were 2 fatals, then I'd want the final chart to hold those two fatal event's descriptions.&lt;/P&gt;

&lt;P&gt;Please let me know if I can clarify further. Thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 05 Sep 2018 15:48:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-use-an-if-statement-to-reduce-rows-of-a-field-in-a/m-p/449504#M127266</guid>
      <dc:creator>samsam48</dc:creator>
      <dc:date>2018-09-05T15:48:24Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use an if-statement to reduce rows of a field in a Splunk result set?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-use-an-if-statement-to-reduce-rows-of-a-field-in-a/m-p/449505#M127267</link>
      <description>&lt;P&gt;Hi samsam48,&lt;BR /&gt;
In this way you don't know if you have 5 descriptions or more, because in both the situation you have 5 description!&lt;BR /&gt;
Anyway, try something like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; sourcetype=GRServerPROD_B FATAL OR ERROR OR WARN 
| rex field=_raw max_match=1 "(?&amp;lt;error_type&amp;gt;WARN|ERROR|FATAL)"  
| rex field=_raw "] (?&amp;lt;error_location&amp;gt;.*?) " 
| rex field=_raw "\) (?&amp;lt;description&amp;gt;.*)"
| stats values(description) AS description count by error_type
| eval description=mvindex(description, 0, 4)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Bye.&lt;BR /&gt;
Giuseppe&lt;/P&gt;</description>
      <pubDate>Thu, 06 Sep 2018 07:26:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-use-an-if-statement-to-reduce-rows-of-a-field-in-a/m-p/449505#M127267</guid>
      <dc:creator>gcusello</dc:creator>
      <dc:date>2018-09-06T07:26:13Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use an if-statement to reduce rows of a field in a Splunk result set?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-use-an-if-statement-to-reduce-rows-of-a-field-in-a/m-p/449506#M127268</link>
      <description>&lt;P&gt;Hi Giuseppe, &lt;/P&gt;

&lt;P&gt;Last thing- how would I go about including the counts and set of 5 descriptions in the alert body? It doesn't look like the counts are "named" so I haven't been able to use the $results.fieldName$ syntax in the email body. Is there also an approach to include all 5 descriptions for each &lt;CODE&gt;error_type&lt;/CODE&gt; in the body as well?&lt;/P&gt;</description>
      <pubDate>Thu, 06 Sep 2018 13:39:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-use-an-if-statement-to-reduce-rows-of-a-field-in-a/m-p/449506#M127268</guid>
      <dc:creator>samsam48</dc:creator>
      <dc:date>2018-09-06T13:39:33Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use an if-statement to reduce rows of a field in a Splunk result set?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-use-an-if-statement-to-reduce-rows-of-a-field-in-a/m-p/449507#M127269</link>
      <description>&lt;P&gt;Hi samsam48,&lt;BR /&gt;
I usually don't insert data in the eMail body to have a more general eMail and to avoid problems with fields.&lt;BR /&gt;
I usually attach a file (csv or pdf) with the results or I enable the link to results (if the receiver is enabled to use Splunk).&lt;/P&gt;

&lt;P&gt;Bye.&lt;BR /&gt;
Giuseppe&lt;/P&gt;</description>
      <pubDate>Thu, 06 Sep 2018 14:55:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-use-an-if-statement-to-reduce-rows-of-a-field-in-a/m-p/449507#M127269</guid>
      <dc:creator>gcusello</dc:creator>
      <dc:date>2018-09-06T14:55:25Z</dc:date>
    </item>
  </channel>
</rss>

