<?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: calculate sum of multiple fields in different lines identified using regex in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206731#M187728</link>
    <description>&lt;P&gt;Thanks for the answer. but the regex selected only truck of a specifc make. &lt;/P&gt;</description>
    <pubDate>Fri, 05 Aug 2016 13:31:46 GMT</pubDate>
    <dc:creator>karthik2146</dc:creator>
    <dc:date>2016-08-05T13:31:46Z</dc:date>
    <item>
      <title>calculate sum of multiple fields in different lines identified using regex</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206727#M187724</link>
      <description>&lt;P&gt;I want to calculate sum of multiple fields which occur in different lines in logs &lt;BR /&gt;
I have logs like &lt;/P&gt;

&lt;P&gt;bmwcar=10&lt;BR /&gt;
bmwtruck=5&lt;BR /&gt;
nissantruck=5&lt;BR /&gt;
renaultcar=4&lt;BR /&gt;
mercedescar=10&lt;BR /&gt;
suzukicar=10&lt;BR /&gt;
tatatruck=5&lt;BR /&gt;
bmwcar=2&lt;BR /&gt;
nissantruck=15&lt;/P&gt;

&lt;P&gt;i want to have timechart  with sum of all cars and sum of all truck, so my output should be car=36, truck=30. &lt;BR /&gt;
i can do it like index="xxxx" sourcetype="web_stats" | timechart span=1d eval (sum(bmwcar)+sum(renaultcar)....etc) but this list is not fixed as a new car can be logged any time in future.  &lt;/P&gt;

&lt;P&gt;so, i am using regex  (.*car) and  (.*truck) but i am not able to sum up all cars together and trucks together.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="xxxx" sourcetype="web_stats"  *car OR *truck | rex "(?&amp;lt;vehicle&amp;gt;(.*car=[\d]+) | (.*truck=[\d]+) )"  | table vehicle, _time | mvexpand vehicle | rex field=vehicle ".*=(?&amp;lt;cnt&amp;gt;(\d+))" | search cnt!=0 | timechart span=1d sum(cnt)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;by the above query, either i can get sum of all cars and trucks together or cars and trucks in a separate chart using separate quereies. but i wanted to have cars and trucks in a same chart in a single query.&lt;BR /&gt;
could you suggest any way to do it?&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 10:29:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206727#M187724</guid>
      <dc:creator>karthik2146</dc:creator>
      <dc:date>2020-09-29T10:29:06Z</dc:date>
    </item>
    <item>
      <title>Re: calculate sum of multiple fields in different lines identified using regex</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206728#M187725</link>
      <description>&lt;P&gt;Try this.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="xxxx" sourcetype="web_stats" *car OR *truck | rex "(?&amp;lt;vehicle&amp;gt;(.*car=[\d]+) | (.*truck=[\d]+) )"  | rex field=vehicle ".*(?&amp;lt;type&amp;gt;[^=]+)=(?&amp;lt;cnt&amp;gt;\d+)" | timechart span=1d sum(cnt) by type
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 Aug 2016 11:46:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206728#M187725</guid>
      <dc:creator>richgalloway</dc:creator>
      <dc:date>2016-08-05T11:46:04Z</dc:date>
    </item>
    <item>
      <title>Re: calculate sum of multiple fields in different lines identified using regex</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206729#M187726</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="xxxx" sourcetype="web_stats" *car OR *truck | rex "(?&amp;lt;make&amp;gt;\w+)(?&amp;lt;type&amp;gt;car|truck)=(?&amp;lt;cnt&amp;gt;\d+)" | timechart span=1d sum(cnt) as total by type
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;*&lt;STRONG&gt;&lt;EM&gt;OR&lt;/EM&gt;&lt;/STRONG&gt;*&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="xxxx" sourcetype="web_stats" *car OR *truck | rex "(?&amp;lt;make&amp;gt;\w+)(?&amp;lt;type&amp;gt;car|truck)=(?&amp;lt;cnt&amp;gt;\d+)" | chart sum(cnt) as total over type by make
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 Aug 2016 12:31:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206729#M187726</guid>
      <dc:creator>sundareshr</dc:creator>
      <dc:date>2016-08-05T12:31:25Z</dc:date>
    </item>
    <item>
      <title>Re: calculate sum of multiple fields in different lines identified using regex</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206730#M187727</link>
      <description>&lt;P&gt;Thanks for answer but sad that it did not work. The regex only selects truck that too one brand.&lt;BR /&gt;
When i did &lt;BR /&gt;
index="xxxx" sourcetype="web_stats" *car OR *truck | rex "(?\w+)(?car|truck)=(?\d+)" | table _time, make, type, cnt&lt;/P&gt;

&lt;P&gt;i get only truck records and make as bmw and their respective count. I dont know the problem, you have any idea?&lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2016 13:29:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206730#M187727</guid>
      <dc:creator>karthik2146</dc:creator>
      <dc:date>2016-08-05T13:29:04Z</dc:date>
    </item>
    <item>
      <title>Re: calculate sum of multiple fields in different lines identified using regex</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206731#M187728</link>
      <description>&lt;P&gt;Thanks for the answer. but the regex selected only truck of a specifc make. &lt;/P&gt;</description>
      <pubDate>Fri, 05 Aug 2016 13:31:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206731#M187728</guid>
      <dc:creator>karthik2146</dc:creator>
      <dc:date>2016-08-05T13:31:46Z</dc:date>
    </item>
    <item>
      <title>Re: calculate sum of multiple fields in different lines identified using regex</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206732#M187729</link>
      <description>&lt;P&gt;Give this a try&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index="xxxx" sourcetype="web_stats"  *car OR *truck | rex "car=(?&amp;lt;car&amp;gt;\d+)" | rex "truck=(?&amp;lt;truck&amp;gt;\d+)" | timechart span=1d sum(car) as car sum(truck) as truck
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 05 Aug 2016 14:41:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206732#M187729</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2016-08-05T14:41:02Z</dc:date>
    </item>
    <item>
      <title>Re: calculate sum of multiple fields in different lines identified using regex</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206733#M187730</link>
      <description>&lt;P&gt;It worked but it selected only few cars and trucks. I now understood why the regex was not working. Actually my log will be posted once in 15 minutes, each log entry will have multiple lines, like this&lt;/P&gt;

&lt;P&gt;bmwcar=10&lt;BR /&gt;
bmwtruck=5&lt;BR /&gt;
nissantruck=5&lt;BR /&gt;
renaultcar=4&lt;/P&gt;

&lt;P&gt;after 15 minutes&lt;/P&gt;

&lt;P&gt;bmwcar=5&lt;BR /&gt;
bmwtruck=4&lt;BR /&gt;
nissantruck=8&lt;BR /&gt;
renaultcar=3&lt;/P&gt;

&lt;P&gt;like this every 15 minutes,&lt;/P&gt;

&lt;P&gt;the regex selects only the first car or truck, so it does not give me correct sum. Could you let me know, how the regex can be modified to select all trucks and cars in each log entry&lt;/P&gt;</description>
      <pubDate>Mon, 08 Aug 2016 13:10:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206733#M187730</guid>
      <dc:creator>karthik2146</dc:creator>
      <dc:date>2016-08-08T13:10:02Z</dc:date>
    </item>
    <item>
      <title>Re: calculate sum of multiple fields in different lines identified using regex</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206734#M187731</link>
      <description>&lt;P&gt;The regex selected only few cars and trucks. I now understood why the regex was not working. Actually my log will be posted once in 15 minutes, each log entry will have multiple lines, like this&lt;/P&gt;

&lt;P&gt;bmwcar=10&lt;BR /&gt;
bmwtruck=5&lt;BR /&gt;
nissantruck=5&lt;BR /&gt;
renaultcar=4&lt;/P&gt;

&lt;P&gt;after 15 minutes&lt;/P&gt;

&lt;P&gt;bmwcar=5&lt;BR /&gt;
bmwtruck=4&lt;BR /&gt;
nissantruck=8&lt;BR /&gt;
renaultcar=3&lt;/P&gt;

&lt;P&gt;like this every 15 minutes,&lt;/P&gt;

&lt;P&gt;the regex selects only the first car or truck, so it does not give me correct sum. Could you let me know, how the regex can be modified to select all trucks and cars in each log entry?&lt;/P&gt;</description>
      <pubDate>Mon, 08 Aug 2016 13:10:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206734#M187731</guid>
      <dc:creator>karthik2146</dc:creator>
      <dc:date>2016-08-08T13:10:13Z</dc:date>
    </item>
    <item>
      <title>Re: calculate sum of multiple fields in different lines identified using regex</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206735#M187732</link>
      <description>&lt;P&gt;Try this&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index="xxxx" sourcetype="web_stats" *car OR *truck | rex field=x max_match=0 "(?&amp;lt;make&amp;gt;\w+)(?&amp;lt;type&amp;gt;car|truck)=(?&amp;lt;cnt&amp;gt;\d+)" | eval z=mvzip(make, mvzip(type, cnt)) | mvexpand z | rex field=z "(?&amp;lt;make&amp;gt;\w+),(?&amp;lt;type&amp;gt;\w+),(?&amp;lt;cnt&amp;gt;\d+)" | stats sum(cnt) as total by type
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Aug 2016 14:02:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206735#M187732</guid>
      <dc:creator>sundareshr</dc:creator>
      <dc:date>2016-08-08T14:02:51Z</dc:date>
    </item>
    <item>
      <title>Re: calculate sum of multiple fields in different lines identified using regex</title>
      <link>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206736#M187733</link>
      <description>&lt;P&gt;Just add max_match=0 to both the rex command. Like this, and that should take care of that.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; index="xxxx" sourcetype="web_stats"  *car OR *truck | rex max_match=0 "car=(?&amp;lt;car&amp;gt;\d+)" | rex max_match=0 "truck=(?&amp;lt;truck&amp;gt;\d+)" | timechart span=1d sum(car) as car sum(truck) as truck
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Aug 2016 14:08:18 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/calculate-sum-of-multiple-fields-in-different-lines-identified/m-p/206736#M187733</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2016-08-08T14:08:18Z</dc:date>
    </item>
  </channel>
</rss>

