<?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: how to dynamically change rex pattern in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/how-to-dynamically-change-rex-pattern/m-p/533301#M150680</link>
    <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/6367"&gt;@bowesmana&lt;/a&gt;, thanks for this. It worked brilliantly.. i will accept this as answer for this question. BUt again, just out of curiosity, is there any way to change rex pattern dynamically? i mean the way i was trying (obviously i ws doing something wrong)?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 16 Dec 2020 09:21:50 GMT</pubDate>
    <dc:creator>vikasverma</dc:creator>
    <dc:date>2020-12-16T09:21:50Z</dc:date>
    <item>
      <title>how to dynamically change rex pattern</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-dynamically-change-rex-pattern/m-p/533194#M150654</link>
      <description>&lt;P&gt;Hello All,&lt;/P&gt;&lt;P&gt;I hope you all are doing well.&lt;/P&gt;&lt;P&gt;I have a situation wherein i have to pass current day value (Sun, Mon, Tue etc) in regex dynamically to capture a value&amp;nbsp; associated which i have in lookup for that day.&lt;/P&gt;&lt;P&gt;I have a lookup, maintenance.csv with below fields.&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;host; maintenance_days&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;host1; Sun=1, Mon=2, Tue=3 and so on&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What i want is, depending on the day on which my search is ran, it should fetch value the corresponding value of the day. For example, if my search runs on Mon, it should return 2, if it runs on Tue, it should return 3 etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I thought i can do this by calculating the day on the search time and passing this as variable in my regex and extracting the value for the day (1, 2, 3 etc) by using fields in rex command but its not working.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Search&lt;/STRONG&gt;:&lt;/P&gt;&lt;P&gt;| inputlookup "maintenance.csv"&lt;BR /&gt;| eval date_wday=strftime(strptime(now(),"%d/%m/%Y"),"%a")&lt;BR /&gt;| rex field=maintenance_days "&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;date_wday&lt;/STRONG&gt;&lt;/FONT&gt;\=(?P&amp;lt;&lt;STRONG&gt;mday&lt;/STRONG&gt;&amp;gt;[^,])"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What i need is, if above search is run on "Mon", then regex in search becomes, "| rex field=maintenance_days "&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;Mon&lt;/STRONG&gt;&lt;/FONT&gt;\=(?P&amp;lt;mday&amp;gt;[^,])"". If it runs on Wednesday, then it becomes "| rex field=maintenance_days "&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;Wed&lt;/STRONG&gt;&lt;/FONT&gt;\=(?P&amp;lt;mday&amp;gt;[^,])"" etc.&lt;/P&gt;&lt;P&gt;I have tried $date_way$ instead of date_wday but it didnt worked. I have tried putting "| rex field=maintenance_days "&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;date_wday&lt;/STRONG&gt;&lt;/FONT&gt;\=(?P&amp;lt;mday&amp;gt;[^,])"" inside a macro and passing "date_wday" as argument, but it again took it as a string instead of field value associated with it.&lt;/P&gt;&lt;P&gt;I did had some sucess in passing field value via map command but i am just wondering if there is any nicer way of doing this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 16:46:10 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-dynamically-change-rex-pattern/m-p/533194#M150654</guid>
      <dc:creator>vikasverma</dc:creator>
      <dc:date>2020-12-15T16:46:10Z</dc:date>
    </item>
    <item>
      <title>Re: how to dynamically change rex pattern</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-dynamically-change-rex-pattern/m-p/533209#M150657</link>
      <description>&lt;P&gt;The $ syntax only works with tokens and the map command.&lt;/P&gt;&lt;P&gt;Try this&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| inputlookup "maintenance.csv"
| eval date_wday=strftime(strptime(now(),"%d/%m/%Y"),"%a")
| rex field=maintenance_days date_wday."\=(?P&amp;lt;mday&amp;gt;[^,])"&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 15 Dec 2020 17:57:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-dynamically-change-rex-pattern/m-p/533209#M150657</guid>
      <dc:creator>richgalloway</dc:creator>
      <dc:date>2020-12-15T17:57:49Z</dc:date>
    </item>
    <item>
      <title>Re: how to dynamically change rex pattern</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-dynamically-change-rex-pattern/m-p/533247#M150662</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/229755"&gt;@vikasverma&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;From your example, is it true that Sun=1 always and Mon=2 always and so on.&lt;/P&gt;&lt;P&gt;If so, then&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval date_wday=tonumber(strftime(now(),"%w"))+1&lt;/LI-CODE&gt;&lt;P&gt;If not, and the numbers are not consistent, then why do you not have your csv with&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;host;Sun;Mon;Tue;Wed;Thu;Fri;Sat
host1;1,2,3,4,5,6,7&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;then you just lookup your day, but I'm not sure if that's what you're after.&lt;/P&gt;&lt;P&gt;An alternative is to do the logic from this search&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval row="host1; Sun=1, Mon=2, Tue=3, Wed=4, Thu=5, Fri=6, Sat=7"
| rex field=row max_match=0 "(?&amp;lt;Day&amp;gt;\w{3})=(?&amp;lt;xday&amp;gt;\d+)"
| eval today=strftime(now(), "%a")
| eval mday=mvindex(xday, mvfind(Day, today))&lt;/LI-CODE&gt;&lt;P&gt;where you are rexing out ALL the day name/value pairs into two multivalue fields, then finding the current day and using that as the offset to the mday values held above in the xday field.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Dec 2020 22:26:04 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-dynamically-change-rex-pattern/m-p/533247#M150662</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2020-12-15T22:26:04Z</dc:date>
    </item>
    <item>
      <title>Re: how to dynamically change rex pattern</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-dynamically-change-rex-pattern/m-p/533301#M150680</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/6367"&gt;@bowesmana&lt;/a&gt;, thanks for this. It worked brilliantly.. i will accept this as answer for this question. BUt again, just out of curiosity, is there any way to change rex pattern dynamically? i mean the way i was trying (obviously i ws doing something wrong)?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 16 Dec 2020 09:21:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-dynamically-change-rex-pattern/m-p/533301#M150680</guid>
      <dc:creator>vikasverma</dc:creator>
      <dc:date>2020-12-16T09:21:50Z</dc:date>
    </item>
    <item>
      <title>Re: how to dynamically change rex pattern</title>
      <link>https://community.splunk.com/t5/Splunk-Search/how-to-dynamically-change-rex-pattern/m-p/533392#M150710</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/229755"&gt;@vikasverma&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;passing variable things to other things is not easy, other than in the field pipeline, but as&amp;nbsp;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/213957"&gt;@richgalloway&lt;/a&gt;&amp;nbsp; pointed out, the map command can do this, but there is another technique where you can do this sort of thing.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults
| eval row="host1; Sun=1, Mon=2, Tue=3, Wed=4, Thu=5, Fri=6, Sat=7"
| eval today=strftime(now(), "%a")
| foreach today [ rex field=row "&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;=(?&amp;lt;mday_rex&amp;gt;\d+)" ]
| foreach today [ eval mday_replace=replace(row,".*".&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;."=(\d+).*", "\1") ]&lt;/LI-CODE&gt;&lt;P&gt;In the above, the foreach statement is used to pass the field value to the subsearch, however, the REX statement does NOT work and mday_rex is null, I don't know why, but the second form, where the replace statement is used, DOES work and the mday_replace field is set correctly. That's just capturing today's day=X value using regex and removing all other text.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 17 Dec 2020 03:21:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/how-to-dynamically-change-rex-pattern/m-p/533392#M150710</guid>
      <dc:creator>bowesmana</dc:creator>
      <dc:date>2020-12-17T03:21:09Z</dc:date>
    </item>
  </channel>
</rss>

