<?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: In a search, how would I get the difference between the primary region and all other regions? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/In-a-search-how-would-I-get-the-difference-between-the-primary/m-p/414741#M119461</link>
    <description>&lt;P&gt;You were almost there, just add single quotes (') around your &lt;CODE&gt;&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;&lt;/CODE&gt; reference and it should work as you expected:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval primary_region = 60 
| eval r_ap-ne = 0 
| eval r_ap-s = 23 
| eval r_us-e = 0 
| eval r_us-w = 0
| foreach r* [ eval d&amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt; = primary_region - '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;' ]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This results in:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;_time               d_ap-ne d_ap-s  d_us-e  d_us-w  primary_region  r_ap-ne r_ap-s  r_us-e  r_us-w
2018-11-29 13:35:31 60      37      60      60          60              0       23      0       0
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Hope this helps&lt;/P&gt;

&lt;P&gt;---EDIT---&lt;BR /&gt;
The reason it fails in your search is because your field names have dashes (-) in them. When Splunk parses that out into the eval, the dash is treated as a mathematical minus so you get the equivalent of  &lt;CODE&gt;primary_region - 'r_ap' - 'northeast' - 1&lt;/CODE&gt;. And neither r_ap, nor northeast are fields that exist. By applying the single quotes, Splunk treats the entire string as a single field name.&lt;/P&gt;</description>
    <pubDate>Thu, 29 Nov 2018 18:38:24 GMT</pubDate>
    <dc:creator>aholzer</dc:creator>
    <dc:date>2018-11-29T18:38:24Z</dc:date>
    <item>
      <title>In a search, how would I get the difference between the primary region and all other regions?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/In-a-search-how-would-I-get-the-difference-between-the-primary/m-p/414740#M119460</link>
      <description>&lt;P&gt;I have this query that is supposed to get the difference between the primary region and all other regions, but for some reason nothing is being returned for d_*&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;   | eval ms_region=rtrim("region_"+ms_zone, "abcdefgh") 
    |chart count OVER tenant_id by ms_region
    |rename region_ap-southeast-1 as "primary_region"
    | rename region* as r*
    |foreach r* [eval d_&amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt;=primary_region - &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;here is my table:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;tenant_id   primary_region  r_ap-northeast-1    r_ap-south-1    r_us-east-1         r_us-west-1
18                 60         0                0                    0                     0
344              370          0                0                    0                     0
366             3505          0                23                   0                    0
441             1323          0                0                    0                       0   
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;My expected result would be to add columns like d_$region1$ d_$region2$, d_$region3$, which would contain the difference of the primary region and other regions.&lt;/P&gt;

&lt;P&gt;I tried debugging it and found out, for some reason, &lt;CODE&gt;&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;&lt;/CODE&gt; in the foreach doesn't return anything.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 22:13:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/In-a-search-how-would-I-get-the-difference-between-the-primary/m-p/414740#M119460</guid>
      <dc:creator>kiamco</dc:creator>
      <dc:date>2020-09-29T22:13:16Z</dc:date>
    </item>
    <item>
      <title>Re: In a search, how would I get the difference between the primary region and all other regions?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/In-a-search-how-would-I-get-the-difference-between-the-primary/m-p/414741#M119461</link>
      <description>&lt;P&gt;You were almost there, just add single quotes (') around your &lt;CODE&gt;&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;&lt;/CODE&gt; reference and it should work as you expected:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval primary_region = 60 
| eval r_ap-ne = 0 
| eval r_ap-s = 23 
| eval r_us-e = 0 
| eval r_us-w = 0
| foreach r* [ eval d&amp;lt;&amp;lt;MATCHSTR&amp;gt;&amp;gt; = primary_region - '&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;' ]
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This results in:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;_time               d_ap-ne d_ap-s  d_us-e  d_us-w  primary_region  r_ap-ne r_ap-s  r_us-e  r_us-w
2018-11-29 13:35:31 60      37      60      60          60              0       23      0       0
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Hope this helps&lt;/P&gt;

&lt;P&gt;---EDIT---&lt;BR /&gt;
The reason it fails in your search is because your field names have dashes (-) in them. When Splunk parses that out into the eval, the dash is treated as a mathematical minus so you get the equivalent of  &lt;CODE&gt;primary_region - 'r_ap' - 'northeast' - 1&lt;/CODE&gt;. And neither r_ap, nor northeast are fields that exist. By applying the single quotes, Splunk treats the entire string as a single field name.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Nov 2018 18:38:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/In-a-search-how-would-I-get-the-difference-between-the-primary/m-p/414741#M119461</guid>
      <dc:creator>aholzer</dc:creator>
      <dc:date>2018-11-29T18:38:24Z</dc:date>
    </item>
    <item>
      <title>Re: In a search, how would I get the difference between the primary region and all other regions?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/In-a-search-how-would-I-get-the-difference-between-the-primary/m-p/414742#M119462</link>
      <description>&lt;P&gt;you can review the foreach documentation &lt;A href="http://docs.splunk.com/Documentation/Splunk/7.2.1/SearchReference/Foreach"&gt;here&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Nov 2018 19:13:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/In-a-search-how-would-I-get-the-difference-between-the-primary/m-p/414742#M119462</guid>
      <dc:creator>cmerriman</dc:creator>
      <dc:date>2018-11-29T19:13:44Z</dc:date>
    </item>
    <item>
      <title>Re: In a search, how would I get the difference between the primary region and all other regions?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/In-a-search-how-would-I-get-the-difference-between-the-primary/m-p/414743#M119463</link>
      <description>&lt;P&gt;Thanks for pointing that out @aholzer , I have been trying to debug this query for hours and totally forgot that it needed a (')&lt;/P&gt;</description>
      <pubDate>Mon, 17 Dec 2018 23:19:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/In-a-search-how-would-I-get-the-difference-between-the-primary/m-p/414743#M119463</guid>
      <dc:creator>kiamco</dc:creator>
      <dc:date>2018-12-17T23:19:30Z</dc:date>
    </item>
  </channel>
</rss>

