<?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 string stored in field to assign value using if in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Use-string-stored-in-field-to-assign-value-using-if/m-p/341358#M101207</link>
    <description>&lt;P&gt;@hsesterhenn_splunk - Well said.&lt;/P&gt;

&lt;P&gt;@ErikaE - You can replace each csv with a more complete search, as long as you replace it with something which effectively returns the values from the same search in each place the csv appears.  It's just easier to read (and to explain) in the simple csv form.&lt;/P&gt;

&lt;P&gt;The first input sensorconditionstest.csv is currently using only the value of testname, but if you need additional data from that test, it will need to come through the query from that side.  The second input  sensorconditionstest.csv is using the testname and the conditions, and you would need to keep it that way for it to operate as designed.  &lt;/P&gt;</description>
    <pubDate>Mon, 24 Apr 2017 13:22:44 GMT</pubDate>
    <dc:creator>DalJeanis</dc:creator>
    <dc:date>2017-04-24T13:22:44Z</dc:date>
    <item>
      <title>Use string stored in field to assign value using if</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Use-string-stored-in-field-to-assign-value-using-if/m-p/341352#M101201</link>
      <description>&lt;P&gt;I am using a search of real-time data and a lookup to check whether certain problems exist based on the data. &lt;/P&gt;

&lt;P&gt;For example: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;SensorA             SensorB            SensorC        ifStringfromLookup 
3                   5                  10             SensorC&amp;lt;3 AND SensorB&amp;lt;1 
3                   5                  10             SensorA&amp;gt;1 AND SensorC&amp;lt;12 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;What I would like to be able to do is check to see if the current sensor values match any of the conditions of interest. &lt;/P&gt;

&lt;P&gt;I've tried&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval Problem=if(ifStringfromLookup, 1, 0)
| eval Problem=if($ifStringfromLookup$, 1, 0) 
| eval Problem=if(tostring(ifStringfromLookup), 1, 0)  
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;And none of these work. I would like the string stored in the field ifStringfromLookup to be used to evaluate the if function. I've tried searching splunk answers and the help but haven't found anything that works. &lt;/P&gt;</description>
      <pubDate>Fri, 21 Apr 2017 16:26:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Use-string-stored-in-field-to-assign-value-using-if/m-p/341352#M101201</guid>
      <dc:creator>ErikaE</dc:creator>
      <dc:date>2017-04-21T16:26:44Z</dc:date>
    </item>
    <item>
      <title>Re: Use string stored in field to assign value using if</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Use-string-stored-in-field-to-assign-value-using-if/m-p/341353#M101202</link>
      <description>&lt;P&gt;I answered this in another Q&amp;amp;A with a &lt;EM&gt;slight&lt;/EM&gt; twist in that the match strings are coming from a lookup file, not inside of events (it pulls the data into events in the beginning so it should be easy for you to track and adjust):&lt;/P&gt;

&lt;P&gt;&lt;A href="https://answers.splunk.com/answers/386488/regex-in-lookuptable.html"&gt;https://answers.splunk.com/answers/386488/regex-in-lookuptable.html&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Apr 2017 16:31:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Use-string-stored-in-field-to-assign-value-using-if/m-p/341353#M101202</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2017-04-21T16:31:48Z</dc:date>
    </item>
    <item>
      <title>Re: Use string stored in field to assign value using if</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Use-string-stored-in-field-to-assign-value-using-if/m-p/341354#M101203</link>
      <description>&lt;P&gt;I'm not familiar with many of the commands you're using in that example, hope you'll forgive the follow up. So I need an if statement that looks something like: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| Problem=if(match(ifStringfromLookup, REGEX goes here), "yes", "no") 
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Where match will return a bool that if can use based on the string provided and the regular expression supplied. &lt;/P&gt;

&lt;P&gt;I'm not sure how to get write the REGEX goes here part based on looking at your example.  I also don't understand if map is required, and if so, what its function is. &lt;/P&gt;</description>
      <pubDate>Fri, 21 Apr 2017 16:53:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Use-string-stored-in-field-to-assign-value-using-if/m-p/341354#M101203</guid>
      <dc:creator>ErikaE</dc:creator>
      <dc:date>2017-04-21T16:53:31Z</dc:date>
    </item>
    <item>
      <title>Re: Use string stored in field to assign value using if</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Use-string-stored-in-field-to-assign-value-using-if/m-p/341355#M101204</link>
      <description>&lt;P&gt;woodcock's method is cool, but for YOUR need, there's a much simpler method.  &lt;/P&gt;

&lt;P&gt;I'm going to assume the following...&lt;/P&gt;

&lt;P&gt;sensorconditionstest.csv contains records with two fields - testname and conditions, populated as follows...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval conditions="SensorC&amp;lt;3 AND SensorB&amp;lt;1!!!!SensorA&amp;gt;1 AND SensorC&amp;lt;12" 
| makemv delim="!!!!" conditions 
| mvexpand conditions 
| streamstats count 
| eval testname="TestX".count 
| table testname conditions
| outputcsv sensorconditionstest.csv
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;sensorreadingtest.csv contains four fields, _time, SensorA, SensorB, SensorC, populated as follows...&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval sensors="A=3,B=5,C=10 A=0,B=0,C=2 A=1,B=5,C=3" | makemv sensors | mvexpand sensors 
| streamstats count as recno 
| makemv delim="," sensors | mvexpand sensors 
| table recno sensors 
| rex field=sensors "(?&amp;lt;sensor&amp;gt;[^,=]+)=(?&amp;lt;reading&amp;gt;[^,]+)" 
| eval {sensor}=reading
| fields - sensor sensors reading 
| stats values(*) as Sensor* by recno
| eval _time =relative_time(now(),"-10m@h") + 60*recno
| table _time Sensor*
| outputcsv sensorreadingtest.csv
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Given the above, this allows you to step through the conditions file and use each record to pass the readings file. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| inputcsv sensorconditionstest.csv
| map maxsearches=10 search="| inputcsv sensorreadingtest.csv | search [| inputcsv sensorconditionstest.csv | where testname = \"$testname$\" | table conditions | rename conditions as search ] | eval testname = \"$testname$\""
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;It would be possible to pass it only once, but you would not know which test or tests had been triggered, and a reading record that triggered multiple tests would only appear once.&lt;/P&gt;</description>
      <pubDate>Fri, 21 Apr 2017 19:27:00 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Use-string-stored-in-field-to-assign-value-using-if/m-p/341355#M101204</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-04-21T19:27:00Z</dc:date>
    </item>
    <item>
      <title>Re: Use string stored in field to assign value using if</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Use-string-stored-in-field-to-assign-value-using-if/m-p/341356#M101205</link>
      <description>&lt;P&gt;I'll have to do some testing to understand if this works. &lt;/P&gt;

&lt;P&gt;Would it change your answer if the sensor readings are coming from an index, not from a csv file?  With the way our instance is configured I'm not sure I have rights to output a csv file, I've never tested it. &lt;/P&gt;

&lt;P&gt;sensorconditionstest.csv has several additional columns which provide further information which I would like to use in the final output of the search. I'd like to use an uploaded file - it has several text columns. Some of the columns are formatted such that, when combined with the sensor data in the index, will either satisfy or not satisfy the if statement. I'd like an output something like this: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; SensorA             SensorB            SensorC        ifStringfromLookup AddtlLookupField Bool
 3                   5                  10             SensorC&amp;lt;3 AND SensorB&amp;lt;1  "fielddata " 0
 3                   5                  10             SensorA&amp;gt;1 AND SensorC&amp;lt;12 "fielddata" 1
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Do I need to make the sensor readings a csv file for this to work? &lt;/P&gt;</description>
      <pubDate>Fri, 21 Apr 2017 20:27:20 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Use-string-stored-in-field-to-assign-value-using-if/m-p/341356#M101205</guid>
      <dc:creator>ErikaE</dc:creator>
      <dc:date>2017-04-21T20:27:20Z</dc:date>
    </item>
    <item>
      <title>Re: Use string stored in field to assign value using if</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Use-string-stored-in-field-to-assign-value-using-if/m-p/341357#M101206</link>
      <description>&lt;P&gt;@DalJeanis That is really cool &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; I hope I get all details right.&lt;BR /&gt;
Keep me honest if I am telling nonsense... &lt;/P&gt;

&lt;P&gt;@ErikaE The magic is the subsearch [ | ... ] by using the "rename"  command, the result of the subsearch is in fact the condition as a searchstring. So you would run all conditions one after the other with every input row of the CSV. If the condition matches you will get the values of SensorA, SensorB and SensorC as a result. &lt;/P&gt;

&lt;P&gt;There used to be a better explanation in older versions of the Docs:&lt;BR /&gt;
&lt;A href="http://docs.splunk.com/Documentation/Splunk/4.3.7/User/HowSubsearchesWork"&gt;http://docs.splunk.com/Documentation/Splunk/4.3.7/User/HowSubsearchesWork&lt;/A&gt;&lt;BR /&gt;
See this great Answers article:&lt;BR /&gt;
&lt;A href="https://answers.splunk.com/answering/7477/view.html"&gt;https://answers.splunk.com/answering/7477/view.html&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Well... it’s explained in “format”...&lt;BR /&gt;
&lt;A href="http://docs.splunk.com/Documentation/Splunk/6.5.3/SearchReference/format"&gt;http://docs.splunk.com/Documentation/Splunk/6.5.3/SearchReference/format&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Try:&lt;BR /&gt;
| inputcsv sensorconditionstest.csv | where testname = "TestX2" | table conditions | rename conditions as search | format&lt;/P&gt;

&lt;P&gt;To answer your first question: Yes, instead of "| inputcsv sensorreading" you can use any search, especially if the data is stored in a Splunk Index.&lt;/P&gt;

&lt;P&gt;And for every column in sensorconditionstest.csv you just have to add an eval like "   | eval testname = \"$testname$\" ". e.g. ' | eval conditions=\"$conditions$\"  ' which would show the filter condition in addition to the name of the condition.&lt;/P&gt;

&lt;P&gt;Does it make sense?&lt;/P&gt;

&lt;P&gt;Holger&lt;/P&gt;</description>
      <pubDate>Sat, 22 Apr 2017 14:35:40 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Use-string-stored-in-field-to-assign-value-using-if/m-p/341357#M101206</guid>
      <dc:creator>hsesterhenn_spl</dc:creator>
      <dc:date>2017-04-22T14:35:40Z</dc:date>
    </item>
    <item>
      <title>Re: Use string stored in field to assign value using if</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Use-string-stored-in-field-to-assign-value-using-if/m-p/341358#M101207</link>
      <description>&lt;P&gt;@hsesterhenn_splunk - Well said.&lt;/P&gt;

&lt;P&gt;@ErikaE - You can replace each csv with a more complete search, as long as you replace it with something which effectively returns the values from the same search in each place the csv appears.  It's just easier to read (and to explain) in the simple csv form.&lt;/P&gt;

&lt;P&gt;The first input sensorconditionstest.csv is currently using only the value of testname, but if you need additional data from that test, it will need to come through the query from that side.  The second input  sensorconditionstest.csv is using the testname and the conditions, and you would need to keep it that way for it to operate as designed.  &lt;/P&gt;</description>
      <pubDate>Mon, 24 Apr 2017 13:22:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Use-string-stored-in-field-to-assign-value-using-if/m-p/341358#M101207</guid>
      <dc:creator>DalJeanis</dc:creator>
      <dc:date>2017-04-24T13:22:44Z</dc:date>
    </item>
  </channel>
</rss>

