<?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: Compare the Date from the Lookup and Write new value. in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-compare-the-date-from-the-lookup-and-write-new-value/m-p/645889#M223622</link>
    <description>&lt;P&gt;Not quite true - you can combine them into a single search using appendpipe - here is a runanywhere example which updates the date if the scores are the same, and returns no results, or returns results if the scores are different without updating the csv&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval score=random()%10
| rename score as saved_score, _time as saved_time
| append 
    [| inputlookup score.csv]
| appendpipe
    [| stats list(saved_time) as saved_time list(saved_score) as saved_score dc(saved_score) as distinct
    | foreach saved_*
        [| eval &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=if(distinct=1, mvindex(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,0),mvindex(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,1))]
    | fields - distinct
    | outputlookup score.csv
    | where 1==2 ]
| stats values(saved_time) as time values(saved_score) as score range(saved_score) as delta
| where delta != 0&lt;/LI-CODE&gt;</description>
    <pubDate>Tue, 06 Jun 2023 10:38:59 GMT</pubDate>
    <dc:creator>ITWhisperer</dc:creator>
    <dc:date>2023-06-06T10:38:59Z</dc:date>
    <item>
      <title>How to compare the date from the lookup and write new value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-compare-the-date-from-the-lookup-and-write-new-value/m-p/645712#M223577</link>
      <description>&lt;P&gt;Hello, Splunkers.&lt;BR /&gt;&lt;STRONG&gt;&lt;BR /&gt;Problem Statement:&lt;/STRONG&gt;&lt;BR /&gt;I've searched the data with "date" and "score" to get the latest data and got the result. (Date may or may not be the current time.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;index=sampledata | head 10 | table Date Score | sort -Date| head 1&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="50%" height="24px"&gt;Date&lt;/TD&gt;
&lt;TD width="50%" height="24px"&gt;Score&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%" height="24px"&gt;2023-02-24&lt;/TD&gt;
&lt;TD width="50%" height="24px"&gt;20&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&lt;BR /&gt;I have created a lookup table "score.csv" to behave like variables to store data.&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="50%" height="24px"&gt;Saved_Date&lt;/TD&gt;
&lt;TD width="50%" height="24px"&gt;Saved_Score&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="50%" height="24px"&gt;2023-01-15&lt;/TD&gt;
&lt;TD width="50%" height="24px"&gt;30&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&lt;BR /&gt;Now, I want to compare something like below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;| eval current_timestamp=strptime(Date, "%Y-%m-%d")
| lookup score.csv Saved_Date &amp;lt;Required Help &amp;gt;
| eval saved_timestamp=strptime(Saved_Date, "%Y-%m-%d")
| eval new=if(current_timestamp &amp;gt; saved_timestamp, "Yes","No")
| where new="Yes"
| &amp;lt;want to overwrite with "Date" and "Score" in score.csv&amp;gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jun 2023 13:12:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-compare-the-date-from-the-lookup-and-write-new-value/m-p/645712#M223577</guid>
      <dc:creator>zen29d</dc:creator>
      <dc:date>2023-06-06T13:12:01Z</dc:date>
    </item>
    <item>
      <title>Re: Compare the Date from the Lookup and Write new value.</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-compare-the-date-from-the-lookup-and-write-new-value/m-p/645726#M223583</link>
      <description>&lt;P&gt;Something like&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=sampledata | head 10 | table Date Score | sort -Date| head 1
| rename Date as Saved_Date, Score as Saved_Score
| append
    [| inputlookup score.csv ``` Saved_Date, Saved_Score ``` ]
| sort - Saved_Date
| head 1
| outputlookup score.csv&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 05 Jun 2023 08:38:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-compare-the-date-from-the-lookup-and-write-new-value/m-p/645726#M223583</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2023-06-05T08:38:49Z</dc:date>
    </item>
    <item>
      <title>Re: Compare the Date from the Lookup and Write new value.</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-compare-the-date-from-the-lookup-and-write-new-value/m-p/645772#M223585</link>
      <description>&lt;P&gt;Thanks for the help, I tried and it worked, and thought it will solve the logic however, I think, I messed up more.&amp;nbsp;&lt;BR /&gt;The logic goes like this.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Current_Date = Date
Current_Score = Score 
Previous_Date = Stored_Date
Previous_Score = Stored_Score

1. Read the Current_Date, Current_Score, Previous_Date, Previous_Score. (Solved)  
1. Compare the Scores: Delta = Current_Score - Previous_Score.
2. If a change is observed: 0&amp;lt;Delta&amp;gt;0, Generate an Alert.
3. If NOT, evaluate the Current_Date and Current_Score and write back the lookup.&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jun 2023 13:08:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-compare-the-date-from-the-lookup-and-write-new-value/m-p/645772#M223585</guid>
      <dc:creator>zen29d</dc:creator>
      <dc:date>2023-06-05T13:08:59Z</dc:date>
    </item>
    <item>
      <title>Re: Compare the Date from the Lookup and Write new value.</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-compare-the-date-from-the-lookup-and-write-new-value/m-p/645869#M223614</link>
      <description>&lt;P&gt;I don't think you can use one command to both do alerting and write back with the logic you just described. &amp;nbsp;So, the two functions have to be in separate searches.&lt;/P&gt;&lt;P&gt;1. Write-back&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=sampledata | head 10 | table Date Score | sort -Date| head 1
| rename Date as Saved_Date, Score as Saved_Score
| append
    [| inputlookup score.csv ``` Saved_Date, Saved_Score ``` ]
| stats list(Saved_Date) as Saved_Date list(Saved_Score) as Saved_Score dc(Saved_Score) as distinct
| foreach Saved_*
    [eval &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt; = if(distinct == 1, mvindex(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;, 0), mvindex(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;, 1)]
| outputlookup score.csv&lt;/LI-CODE&gt;&lt;P&gt;In the above, if the two scores are different, simply write back the values from lookup itself.&lt;/P&gt;&lt;P&gt;2. Alerting&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;index=sampledata | head 10 | table Date Score | sort -Date| head 1
| append
    [| inputlookup score.csv ``` Saved_Date, Saved_Score ``` ]
| stats values(Date) as Date values(Saved_Date) as Saved_Date values(Score) as Score values(Saved_Score) as Saved_Score
| eval Delta = Score - Saved_Score
| where Delta != 0&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 06 Jun 2023 08:15:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-compare-the-date-from-the-lookup-and-write-new-value/m-p/645869#M223614</guid>
      <dc:creator>yuanliu</dc:creator>
      <dc:date>2023-06-06T08:15:29Z</dc:date>
    </item>
    <item>
      <title>Re: Compare the Date from the Lookup and Write new value.</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-compare-the-date-from-the-lookup-and-write-new-value/m-p/645889#M223622</link>
      <description>&lt;P&gt;Not quite true - you can combine them into a single search using appendpipe - here is a runanywhere example which updates the date if the scores are the same, and returns no results, or returns results if the scores are different without updating the csv&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval score=random()%10
| rename score as saved_score, _time as saved_time
| append 
    [| inputlookup score.csv]
| appendpipe
    [| stats list(saved_time) as saved_time list(saved_score) as saved_score dc(saved_score) as distinct
    | foreach saved_*
        [| eval &amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=if(distinct=1, mvindex(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,0),mvindex(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,1))]
    | fields - distinct
    | outputlookup score.csv
    | where 1==2 ]
| stats values(saved_time) as time values(saved_score) as score range(saved_score) as delta
| where delta != 0&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 06 Jun 2023 10:38:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-compare-the-date-from-the-lookup-and-write-new-value/m-p/645889#M223622</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2023-06-06T10:38:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare the date from the Lookup and Write new value?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-compare-the-date-from-the-lookup-and-write-new-value/m-p/645892#M223624</link>
      <description>&lt;P&gt;As an additional hint not directly related to the main issue - it's often better to store the timestamp as a unix timestamp than a string rendition of it. It's easier to manipulate, calculate offsets and so on. You only render it to string when displaying.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jun 2023 10:51:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-compare-the-date-from-the-lookup-and-write-new-value/m-p/645892#M223624</guid>
      <dc:creator>PickleRick</dc:creator>
      <dc:date>2023-06-06T10:51:10Z</dc:date>
    </item>
  </channel>
</rss>

