<?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 have 'count as' and also 'count by' for one search in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-have-count-as-and-also-count-by-for-one-search/m-p/362110#M160609</link>
    <description>&lt;P&gt;It's done/working. I am a noob here, meant to post as a comment to previous answer &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 23 Jun 2017 15:48:20 GMT</pubDate>
    <dc:creator>jcunningham_con</dc:creator>
    <dc:date>2017-06-23T15:48:20Z</dc:date>
    <item>
      <title>How to have 'count as' and also 'count by' for one search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-have-count-as-and-also-count-by-for-one-search/m-p/362103#M160602</link>
      <description>&lt;P&gt;The following query should be intuitive enough to see what am trying to do. This query will list Success_file field values as desired, however the eval will fail. On the other hand, if I replace 'count by' with 'count as' and include values(Success_File) then the eval will work correctly, but the grouping does not and thus duplicate field values are not listed etc. Is there a way to do both so that the below eval command will succeed?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=prod sourcetype=esb ("bulk process for file" AND "xxx.yyy")
| rex field=_raw "bulk process for file: (?&amp;lt;Success_File&amp;gt;.*)" 
| replace "*is successful completed" with * in Success_File
| stats count by Success_File
| eventstats sum(count) as Success_Count
| eval Success_File = if(Success_Count=0, "No Success File Today", Success_File) 
| fields Success_File 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 29 Sep 2020 14:34:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-have-count-as-and-also-count-by-for-one-search/m-p/362103#M160602</guid>
      <dc:creator>jcunningham_con</dc:creator>
      <dc:date>2020-09-29T14:34:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to have 'count as' and also 'count by' for one search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-have-count-as-and-also-count-by-for-one-search/m-p/362104#M160603</link>
      <description>&lt;P&gt;After stats command, you'll have different Success_File and corresponding count. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Success_File count
..............................
file1   count1
file2   count2
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The eventstats will add a column with sum of all count values and will never be 0. What exactly is your requirement here? Do you want to show a rows with Success_File="No Success File Today" with count=0 when your search doesn't return any data (no files being processed)? If yes, try this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=prod sourcetype=esb ("bulk process for file" AND "xxx.yyy")
 | rex field=_raw "bulk process for file: (?&amp;lt;Success_File&amp;gt;.*)is successful completed" 
 | stats count by Success_File
 | appendpipe[ | stats count | where count=0 | eval Success_File="No Success File Today"]
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 22 Jun 2017 21:26:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-have-count-as-and-also-count-by-for-one-search/m-p/362104#M160603</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2017-06-22T21:26:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to have 'count as' and also 'count by' for one search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-have-count-as-and-also-count-by-for-one-search/m-p/362105#M160604</link>
      <description>&lt;P&gt;I totally do not get the problem but &lt;EM&gt;maybe&lt;/EM&gt; the problem is that you have values for &lt;CODE&gt;Success_File&lt;/CODE&gt; that sometimes don't exist and you would like it to always exists but to show "count=0" instead of disappearing.  Is that it?  If not, then you need to back way up and explain it in a different way, because I don't get it.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Jun 2017 21:27:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-have-count-as-and-also-count-by-for-one-search/m-p/362105#M160604</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2017-06-22T21:27:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to have 'count as' and also 'count by' for one search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-have-count-as-and-also-count-by-for-one-search/m-p/362106#M160605</link>
      <description>&lt;P&gt;First, ALWAYS rename &lt;CODE&gt;count&lt;/CODE&gt; using &lt;CODE&gt;count as&lt;/CODE&gt;.  It saves all kinds of mistakes.&lt;/P&gt;

&lt;P&gt;Second, If there are no records, then &lt;CODE&gt;count by&lt;/CODE&gt; produces nothing.  You can add a record a number of ways, but in this case using appendpipe is an easy choice.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index=prod sourcetype=esb ("bulk process for file" AND "xxx.yyy")
 | rex field=_raw "bulk process for file: (?&amp;lt;Success_File&amp;gt;.*)" 
 | replace "*is successful completed" with * in Success_File
 | stats count as mycount by Success_File

 | rename COMMENT as "Now add a record for no files, but only if there are no records to this point."
 | appendpipe [| stats count as mycount | where cmyount==0 | eval Success_File = "No Success Files Today" ] 

 | rename COMMENT as "... and eliminate everything but the file names, since there can be only one."
 | fields Success_File 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;HR /&gt;

&lt;P&gt;It also seems like the first chunk could be replaced by ...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index=prod sourcetype=esb ("bulk process for file" AND "xxx.yyy")
 | rex field=_raw "bulk process for file: (?&amp;lt;Success_File&amp;gt;.*?) is successful(ly)? completed" 
 | stats count as mycount by Success_File
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 22 Jun 2017 21:27:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-have-count-as-and-also-count-by-for-one-search/m-p/362106#M160605</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-06-22T21:27:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to have 'count as' and also 'count by' for one search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-have-count-as-and-also-count-by-for-one-search/m-p/362107#M160606</link>
      <description>&lt;P&gt;The way I read the question and the code, @woodcock and @somesoni2, OP has different success file names, there will only ever be one count per file, and OP wants a single record to say "no files" if there are none.&lt;/P&gt;</description>
      <pubDate>Thu, 22 Jun 2017 21:31:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-have-count-as-and-also-count-by-for-one-search/m-p/362107#M160606</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-06-22T21:31:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to have 'count as' and also 'count by' for one search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-have-count-as-and-also-count-by-for-one-search/m-p/362108#M160607</link>
      <description>&lt;P&gt;Thanks for the help, makes sense now. Some additional info about the problem is that there is a file processed potentially daily and currently we are doing manual checks. Instead, we are setting up a Splunk alert to provide a report for each hop of the file processing. If the file was successfully transferred; display file name. If there was a corresponding error file created, display the name of it and how many error records there were, and etc. Following is scrubbed query for two of the hops:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=prod sourcetype=esb ("bulk process for file" AND "xxx.yyyy")
| rex field=_raw "bulk process for file: (?&amp;lt;esb_Success_File&amp;gt;.*?) is successful(ly)? completed" 
| stats count as mycount by esb_Success_File
| rename COMMENT as "Now add a record for no files, but only if there are no records to this point."
  | appendpipe [| stats count as mycount | where mycount==0 | eval esb_Success_File = "No Success Files at Hop2 Today" ] 
  | rename COMMENT as "... and eliminate everything but the file names, since there can be only one."

| appendcols [search index="prod" sourcetype="esb" ("Error file" AND "xxx.yyyyz*") 
| rex field=_raw "Error file (?&amp;lt;esb_Error_File&amp;gt;.*)" 
| rex mode=sed field=esb_Error_File "s/records: (\d+)/{records}/" 
| rex mode=sed field=esb_Error_File "s/out of (\d+)/{out of total}/"
| replace "/xxx/yyyy/dddd/hhhhhh/ffffff/* is created with total error {records} {out of total} records" with * in esb_Error_File
| rex field=_raw "is created with total error records: (?&amp;lt;esb_Error_Records&amp;gt;.*)"
| stats count as mycount by esb_Error_File esb_Error_Records 
| appendpipe [| stats count as mycount | where mycount==0 | eval esb_Error_File = "No Error Files at Hop2 Today", esb_Error_Records = "N/A" ]]

| appendcols [search index="prod" sourcetype="file_transfer" ("the new filename is" AND "xxx.yyyy")
| rex field=_raw "the new filename is (?&amp;lt;FT_Success_File&amp;gt;xxx.*)"
| stats count as mycount by FT_Success_File 
| appendpipe [| stats count as mycount | where mycount==0 | eval FT_Success_File = "File Not Sent from Hop1 during this time"]]

| appendcols [search index="prod" sourcetype="file_transfer" ("xxx.yyyy" AND "Successful transfer")
| rex field=_raw "File=/xxxx/yyyyyyy/ddd.fff.vvv.(?&amp;lt;FT_Transfer_Status&amp;gt;xxx.yyyy.*\n.*)"
| stats count as mycount by FT_Transfer_Status
| appendpipe [| stats count as mycount | where mycount==0 | eval FT_Transfer_Status = "File Not Sent from Hop1 during this time"]]

| fields FT_Success_File FT_Transfer_Status esb_Success_File esb_Error_File esb_Error_Records 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 23 Jun 2017 15:04:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-have-count-as-and-also-count-by-for-one-search/m-p/362108#M160607</guid>
      <dc:creator>jcunningham_con</dc:creator>
      <dc:date>2017-06-23T15:04:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to have 'count as' and also 'count by' for one search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-have-count-as-and-also-count-by-for-one-search/m-p/362109#M160608</link>
      <description>&lt;P&gt;So are you done or looking for more help (a different answer)?&lt;/P&gt;</description>
      <pubDate>Fri, 23 Jun 2017 15:40:55 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-have-count-as-and-also-count-by-for-one-search/m-p/362109#M160608</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2017-06-23T15:40:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to have 'count as' and also 'count by' for one search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-have-count-as-and-also-count-by-for-one-search/m-p/362110#M160609</link>
      <description>&lt;P&gt;It's done/working. I am a noob here, meant to post as a comment to previous answer &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Jun 2017 15:48:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-have-count-as-and-also-count-by-for-one-search/m-p/362110#M160609</guid>
      <dc:creator>jcunningham_con</dc:creator>
      <dc:date>2017-06-23T15:48:20Z</dc:date>
    </item>
    <item>
      <title>Re: How to have 'count as' and also 'count by' for one search</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-have-count-as-and-also-count-by-for-one-search/m-p/362111#M160610</link>
      <description>&lt;P&gt;Converted.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Jun 2017 16:21:37 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-have-count-as-and-also-count-by-for-one-search/m-p/362111#M160610</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2017-06-23T16:21:37Z</dc:date>
    </item>
  </channel>
</rss>

