<?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: Conditional Search items in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Conditional-Search-items/m-p/321051#M95904</link>
    <description>&lt;P&gt;Give this a try&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=example src=$ip$ | where NOT ((cidrmatch("10.0.0.0/8",$ip$) OR cidrmatch("172.0.0.0/8",$ip$) AND (dest="10.0.0.0/8" OR  dest!="172.0.0.0/8")) | ...rest of the search...
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;OR&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=example src=$ip$ | where NOT [| gentimes start=-1 | eval dest=if((cidrmatch("10.0.0.0/8","$ip$") OR cidrmatch("172.0.0.0/8","$ip$")),"10.0.0.0/8#172.0.0.0/8","*") | table dest | makemv dest delim="#" | mvexpand dest ] | ..rest of the search
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 20 Jul 2017 18:38:26 GMT</pubDate>
    <dc:creator>somesoni2</dc:creator>
    <dc:date>2017-07-20T18:38:26Z</dc:date>
    <item>
      <title>Conditional Search items</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Conditional-Search-items/m-p/321049#M95902</link>
      <description>&lt;P&gt;I'm trying to create a conditional which will search using one of two search terms based on an IF statement.&lt;/P&gt;

&lt;P&gt;A simplified example of what I'm trying to do looks like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;IF "(condition)",then, "Search1", else, "Search2"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;For context, I'm trying to check to see if an IP matches a CIDR range (private address). If there's a match, execute Search1. If not, execute Search2.&lt;/P&gt;

&lt;P&gt;I'm new to SPL, coming from a scripting background, so I'm not sure if this method is even possible in Splunk.&lt;/P&gt;

&lt;P&gt;Thank you!&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;Edit: Here is more information&lt;/STRONG&gt;&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;The IF statement&lt;/STRONG&gt; (checking if token $ip$ is an internal address:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;if((cidrmatch("10.0.0.0/8",$ip$) OR cidrmatch("172.0.0.0/8",$ip$)), Search1, else Search2)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;Search1&lt;/STRONG&gt; (filtering out other internal addresses, if token $ip$ is an internal address):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=example src=$ip$ | where dest!="10.0.0.0/8" | where dest!="172.0.0.0/8"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;STRONG&gt;Search 2&lt;/STRONG&gt; (not filtering internal addresses, if token $ip$ is an external address):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=example src=$ip$
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I've excluded the sorting commands etc that will be appended, since I do not believe they would affect the logic here.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 14:15:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Conditional-Search-items/m-p/321049#M95902</guid>
      <dc:creator>danataylor</dc:creator>
      <dc:date>2017-07-20T14:15:17Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Search items</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Conditional-Search-items/m-p/321050#M95903</link>
      <description>&lt;P&gt;We can help better if you could post both your searches.  &lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 17:24:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Conditional-Search-items/m-p/321050#M95903</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2017-07-20T17:24:31Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Search items</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Conditional-Search-items/m-p/321051#M95904</link>
      <description>&lt;P&gt;Give this a try&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=example src=$ip$ | where NOT ((cidrmatch("10.0.0.0/8",$ip$) OR cidrmatch("172.0.0.0/8",$ip$) AND (dest="10.0.0.0/8" OR  dest!="172.0.0.0/8")) | ...rest of the search...
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;OR&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;sourcetype=example src=$ip$ | where NOT [| gentimes start=-1 | eval dest=if((cidrmatch("10.0.0.0/8","$ip$") OR cidrmatch("172.0.0.0/8","$ip$")),"10.0.0.0/8#172.0.0.0/8","*") | table dest | makemv dest delim="#" | mvexpand dest ] | ..rest of the search
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 Jul 2017 18:38:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Conditional-Search-items/m-p/321051#M95904</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2017-07-20T18:38:26Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Search items</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Conditional-Search-items/m-p/321052#M95905</link>
      <description>&lt;P&gt;Your second solution works! Thank you. However, I don't know how it works. Could you break down these steps for me? &lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 19:15:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Conditional-Search-items/m-p/321052#M95905</guid>
      <dc:creator>danataylor</dc:creator>
      <dc:date>2017-07-20T19:15:58Z</dc:date>
    </item>
    <item>
      <title>Re: Conditional Search items</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Conditional-Search-items/m-p/321053#M95906</link>
      <description>&lt;P&gt;The where subsearch is basically drawing down the dest field filter and returning to main search.&lt;BR /&gt;
a) If &lt;CODE&gt;(cidrmatch("10.0.0.0/8","$ip$") OR cidrmatch("172.0.0.0/8","$ip$"))&lt;/CODE&gt; is true, the subsearch is setting the value of dest as &lt;CODE&gt;10.0.0.0/8#172.0.0.0/8&lt;/CODE&gt;, splitting them out and final output will be (including where clause) &lt;CODE&gt;| where NOT (dest="10.0.0.0/8" OR dest="172.0.0.0/8")&lt;/CODE&gt;. &lt;BR /&gt;
b) if &lt;CODE&gt;(cidrmatch("10.0.0.0/8","$ip$") OR cidrmatch("172.0.0.0/8","$ip$"))&lt;/CODE&gt; is false, the subsearch is setting the value of dest as &lt;CODE&gt;*&lt;/CODE&gt; and final output will be (including where clause) &lt;CODE&gt;| where NOT (dest="*")&lt;/CODE&gt;. If I think again, you can use any junk character/string instead of *. The purpose here should be ensure that filter does't match against any records and does no filter.&lt;/P&gt;</description>
      <pubDate>Thu, 20 Jul 2017 19:58:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Conditional-Search-items/m-p/321053#M95906</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2017-07-20T19:58:34Z</dc:date>
    </item>
  </channel>
</rss>

