<?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: Use a field as the search in searchmatch in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Use-a-field-as-the-search-in-searchmatch/m-p/363132#M165124</link>
    <description>&lt;P&gt;Hi triest,&lt;/P&gt;

&lt;P&gt;maybe I misunderstand your question, but how about a &lt;CODE&gt;case()&lt;/CODE&gt; instead of &lt;CODE&gt;searchmatch()&lt;/CODE&gt;?&lt;BR /&gt;
Since &lt;CODE&gt;searchmatch()&lt;/CODE&gt;takes a regex as argument you will compare against a literal &lt;CODE&gt;filter&lt;/CODE&gt; in your example.&lt;BR /&gt;
Your example works btw if you do it like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; | eval actual=if(searchmatch("filter"),1,0)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;but this will match all events since you have &lt;CODE&gt;filter&lt;/CODE&gt; in all example events.&lt;/P&gt;

&lt;P&gt;Would a &lt;CODE&gt;case()&lt;/CODE&gt; like this do the thing?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; | eval actual=case(expected="1", "Yes", expected="0", "No", 1=1, "unknown")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Again I may understand the requirement completely wrong &lt;CODE&gt;¯\_(ツ)_/¯&lt;/CODE&gt; &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;cheers, MuS &lt;/P&gt;</description>
    <pubDate>Wed, 15 Nov 2017 18:13:23 GMT</pubDate>
    <dc:creator>MuS</dc:creator>
    <dc:date>2017-11-15T18:13:23Z</dc:date>
    <item>
      <title>Use a field as the search in searchmatch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Use-a-field-as-the-search-in-searchmatch/m-p/363131#M165123</link>
      <description>&lt;P&gt;I would like to use a field as the string for searchmatch, but that results in an error stating:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;Error in 'eval' command: The arguments to the 'searchmatch' function are invalid.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;I've attempted to recreate the issue using fake data to demonstrate the issue. &lt;/P&gt;

&lt;P&gt;Assuming I have data like the results from the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; | makeresults
 | eval data="index=foo|foo|1;index=foo|bar|0;index=bar|bar|1;index=bar|foo|0;index=foo OR index=bar|foo|1;index=foo OR index=bar|bar|1"
 | fields - _time
 | eval data=split(data, ";")
 | mvexpand data
 | rex field=data "^(?&amp;lt;filter&amp;gt;[^\|]+)\|(?&amp;lt;index&amp;gt;[^\|]+)\|(?&amp;lt;expected&amp;gt;[^\|]+)$"
 | fields filter index expected
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I'd really like to be able to append &lt;CODE&gt;| eval actual=if(searchmatch(filter),1,0)&lt;/CODE&gt; and have actual match the expected column&lt;/P&gt;

&lt;P&gt;I can copy the string from filter into search match and run them individually, but I need something that scales.&lt;/P&gt;

&lt;P&gt;EDIT:&lt;BR /&gt;
Just to clarify, the example I provided is fairly simple in that it is only filtering by index, but in my actual data I have all sorts of filters so something that parses out the indexes and then checks won't be sufficient for my needs.&lt;/P&gt;

&lt;P&gt;Also in my actual data, I don't have the expected column.  I added that here just to indicate if I assumed the data should match.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Nov 2017 17:33:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Use-a-field-as-the-search-in-searchmatch/m-p/363131#M165123</guid>
      <dc:creator>triest</dc:creator>
      <dc:date>2017-11-15T17:33:49Z</dc:date>
    </item>
    <item>
      <title>Re: Use a field as the search in searchmatch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Use-a-field-as-the-search-in-searchmatch/m-p/363132#M165124</link>
      <description>&lt;P&gt;Hi triest,&lt;/P&gt;

&lt;P&gt;maybe I misunderstand your question, but how about a &lt;CODE&gt;case()&lt;/CODE&gt; instead of &lt;CODE&gt;searchmatch()&lt;/CODE&gt;?&lt;BR /&gt;
Since &lt;CODE&gt;searchmatch()&lt;/CODE&gt;takes a regex as argument you will compare against a literal &lt;CODE&gt;filter&lt;/CODE&gt; in your example.&lt;BR /&gt;
Your example works btw if you do it like this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; | eval actual=if(searchmatch("filter"),1,0)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;but this will match all events since you have &lt;CODE&gt;filter&lt;/CODE&gt; in all example events.&lt;/P&gt;

&lt;P&gt;Would a &lt;CODE&gt;case()&lt;/CODE&gt; like this do the thing?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; | eval actual=case(expected="1", "Yes", expected="0", "No", 1=1, "unknown")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Again I may understand the requirement completely wrong &lt;CODE&gt;¯\_(ツ)_/¯&lt;/CODE&gt; &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;cheers, MuS &lt;/P&gt;</description>
      <pubDate>Wed, 15 Nov 2017 18:13:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Use-a-field-as-the-search-in-searchmatch/m-p/363132#M165124</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2017-11-15T18:13:23Z</dc:date>
    </item>
    <item>
      <title>Re: Use a field as the search in searchmatch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Use-a-field-as-the-search-in-searchmatch/m-p/363133#M165125</link>
      <description>&lt;P&gt;@MuS,&lt;/P&gt;

&lt;P&gt;You are completely correct that in this simple case that would work.  I added the expected to show if I thought the filter should match the event or not; in the real data set I wouldn't have that.  I'll try to clarify the question; I really appreciate you trying.&lt;/P&gt;</description>
      <pubDate>Wed, 15 Nov 2017 18:33:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Use-a-field-as-the-search-in-searchmatch/m-p/363133#M165125</guid>
      <dc:creator>triest</dc:creator>
      <dc:date>2017-11-15T18:33:25Z</dc:date>
    </item>
    <item>
      <title>Re: Use a field as the search in searchmatch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Use-a-field-as-the-search-in-searchmatch/m-p/363134#M165126</link>
      <description>&lt;P&gt;Hi @triest,&lt;/P&gt;

&lt;P&gt;If I understand correctly, then try appending the following as I think this may do what you want:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval actual=if(searchmatch("expected=1"), filter, "")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Note the quotes inside the searchmatch() - without which you'd get "Error in 'eval' command."&lt;/P&gt;

&lt;P&gt;I've appended it to your query, which gave the following output when I ran it, with the 'actual' column showing the filters that matched, or empty if they didn't:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;filter                  index   expected    actual
index=foo               foo     1           index=foo
index=foo               bar     0           
index=bar               bar     1           index=bar
index=bar               foo     0           
index=foo OR index=bar  foo     1           index=foo OR index=bar
index=foo OR index=bar  bar     1           index=foo OR index=bar
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Is this what you were trying to do?&lt;/P&gt;</description>
      <pubDate>Thu, 31 May 2018 01:49:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Use-a-field-as-the-search-in-searchmatch/m-p/363134#M165126</guid>
      <dc:creator>rojyates</dc:creator>
      <dc:date>2018-05-31T01:49:11Z</dc:date>
    </item>
    <item>
      <title>Re: Use a field as the search in searchmatch</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Use-a-field-as-the-search-in-searchmatch/m-p/363135#M165127</link>
      <description>&lt;P&gt;&lt;STRONG&gt;searchmatch&lt;/STRONG&gt; will not allow a field to be used in place of &lt;EM&gt;string&lt;/EM&gt;. However, the &lt;STRONG&gt;match&lt;/STRONG&gt; function of eval will, and &lt;STRONG&gt;match&lt;/STRONG&gt; can be made to behave like &lt;STRONG&gt;searchmatch&lt;/STRONG&gt; very easily!&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;| eval searchHits=if(match(_raw,"Type=Error"),1,0)&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;is the same as:&lt;/P&gt;

&lt;P&gt;&lt;CODE&gt;| eval searchHits=if(searchmatch("Type=Error"),1,0)&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;Further, &lt;STRONG&gt;match&lt;/STRONG&gt; will support the regex pipe, so you can OR as well. Here's an example of both:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;... | eval typeField = "Type=Error|Type=Warning"
| eval searchHits=if(match(_raw,typeField),1,0)
| stats count as EventCount sum(searchHits) AS searchHits by Type
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Jun 2018 17:25:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Use-a-field-as-the-search-in-searchmatch/m-p/363135#M165127</guid>
      <dc:creator>wrangler2x</dc:creator>
      <dc:date>2018-06-22T17:25:43Z</dc:date>
    </item>
  </channel>
</rss>

