<?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: Pass a variable to a rex command in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337009#M100013</link>
    <description>&lt;P&gt;@JohannesGmelin, hope this worked for you. Let me convert this to answer so that you can accept and mark your question as answered.&lt;/P&gt;</description>
    <pubDate>Wed, 07 Jun 2017 09:09:48 GMT</pubDate>
    <dc:creator>niketn</dc:creator>
    <dc:date>2017-06-07T09:09:48Z</dc:date>
    <item>
      <title>Pass a variable to a rex command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337000#M100004</link>
      <description>&lt;P&gt;Hey Community,&lt;/P&gt;

&lt;P&gt;I'm trying to pass a variable including the pattern to a rex command mode=sed.&lt;BR /&gt;
This is my approach but it doesn't work. My screen just give me a message: Search is waiting for input. &lt;BR /&gt;
Whats wrong with my code? &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| eval rex_langing_page=if("$landing_page$"=="*", "s/^(\/[^\/]+\/).*$/\1/", ".*") //This should be the pattern for my rex command
| rex field=Web.uri mode=sed "$rex_landing_page$"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This is my whole code:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| tstats summariesonly=t count FROM datamodel=Web WHERE Web.site="$site$" "Web.eventtype"=pageview "Web.http_session_pageviews"&amp;gt;1 GROUPBY "Web.site","Web.http_session",_time span=1s,"Web.http_referer","Web.uri","Web.http_session_channel"
| eval rex_langing_page=if("$landing_page$"=="*", "s/^(\/[^\/]+\/).*$/\1/", ".*")
| rex field=Web.uri mode=sed "$rex_landing_page$"
| rename Web.uri AS to "Web.http_referer" AS from "Web.http_session_channel" AS channel "Web.site" AS site "Web.http_session" AS http_session
| rex field=from "https?://.+?(?&amp;lt;from_path&amp;gt;/[^\\?]+)" 
| rename from_path AS from
| eval from=if(isnull(from),"$site$",from)
| streamstats global=f count AS interaction by http_session 
| eval interaction=interaction-1
| where interaction&amp;lt;=round($limit$/20,0)
| stats list(from) AS from, list(to) AS to,earliest(to) AS landing_page, list(channel) as channel, list(interaction) as interaction by http_session
| search to="$to$" landing_page="$landing_page$"
| eval fields = mvzip(from,mvzip(to,mvzip(interaction,channel))) 
| fields http_session fields
| mvexpand fields
| rex field=fields "(?&amp;lt;from&amp;gt;[^\,]+),(?&amp;lt;to&amp;gt;[^\,]+),(?&amp;lt;interaction&amp;gt;[^\,]+),(?&amp;lt;channel&amp;gt;.+)"
| eval from=if(interaction=0, "", from)
| eval from=if("$landing_page$"!="*", "$site$"."$landing_page$", from)
| table http_session from to interaction
| where interaction&amp;gt;=1
| where to!="$landing_page$"
| where to!=from
| stats count by from,to,interaction
| sort interaction,-count,from,to
| streamstats global=f count AS interaction_rank by interaction
| where interaction_rank&amp;lt;=round($limit$/10,0)
| stats sum(count) AS count by from,to
| sort from to count
| head $limit$
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Thanks a lot&lt;BR /&gt;
Johannes &lt;/P&gt;</description>
      <pubDate>Tue, 06 Jun 2017 07:46:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337000#M100004</guid>
      <dc:creator>JohannesGmelin</dc:creator>
      <dc:date>2017-06-06T07:46:57Z</dc:date>
    </item>
    <item>
      <title>Re: Pass a variable to a rex command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337001#M100005</link>
      <description>&lt;P&gt;Which version of Splunk are you using? Also where are you setting the &lt;STRONG&gt;$landing_page$&lt;/STRONG&gt; token? Would it be possible for you to use &lt;STRONG&gt;eval&lt;/STRONG&gt; instead of &lt;STRONG&gt;set&lt;/STRONG&gt; to set the token for regular expression directly?&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jun 2017 08:03:29 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337001#M100005</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-06-06T08:03:29Z</dc:date>
    </item>
    <item>
      <title>Re: Pass a variable to a rex command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337002#M100006</link>
      <description>&lt;P&gt;The value $landing_page" is coming from a dropdown filter. I need this because I want to set different patterns to my rex according to the value in my dropdown filter.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;input type="dropdown" token="landing_page" searchWhenChanged="true"&amp;gt;
      &amp;lt;label&amp;gt;Landing page&amp;lt;/label&amp;gt;
      &amp;lt;choice value="*"&amp;gt;Any&amp;lt;/choice&amp;gt;
      &amp;lt;search&amp;gt;
        &amp;lt;query&amp;gt;| tstats summariesonly=t count FROM datamodel=Web WHERE Web.site="$site$" "Web.eventtype"=pageview GROUPBY "Web.site","Web.http_session","Web.uri" 
| stats last("Web.uri") AS landing_page by "Web.http_session" 
| rex field=landing_page mode=sed "s/^(\/[^\/]+\/).*$/\1/"
| top limit=20 landing_page 
| eval landing_page_description=landing_page+" ("+count+")" 
| fields landing_page landing_page_description&amp;lt;/query&amp;gt;
        &amp;lt;earliest&amp;gt;0&amp;lt;/earliest&amp;gt;
        &amp;lt;latest&amp;gt;now&amp;lt;/latest&amp;gt;
      &amp;lt;/search&amp;gt;
      &amp;lt;fieldForLabel&amp;gt;landing_page_description&amp;lt;/fieldForLabel&amp;gt;
      &amp;lt;fieldForValue&amp;gt;landing_page&amp;lt;/fieldForValue&amp;gt;
      &amp;lt;default&amp;gt;*&amp;lt;/default&amp;gt;
    &amp;lt;/input&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 06 Jun 2017 08:08:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337002#M100006</guid>
      <dc:creator>JohannesGmelin</dc:creator>
      <dc:date>2017-06-06T08:08:14Z</dc:date>
    </item>
    <item>
      <title>Re: Pass a variable to a rex command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337003#M100007</link>
      <description>&lt;P&gt;&lt;a href="https://community.splunk.com/t5/user/viewprofilepage/user-id/41005"&gt;@JohannesGmelin&lt;/a&gt;, you can try the following on &lt;CODE&gt;&amp;lt;change&amp;gt;&lt;/CODE&gt; event to your dropdown as shown below. &lt;CODE&gt;&amp;lt;eval&amp;gt;&lt;/CODE&gt; will set the &lt;STRONG&gt;$rex_landing_page$&lt;/STRONG&gt; token based on dropdown selection, which you can use in your query:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; &amp;lt;input type="dropdown" token="landing_page" searchWhenChanged="true"&amp;gt;
       &amp;lt;label&amp;gt;Landing page&amp;lt;/label&amp;gt;
       &amp;lt;choice value="*"&amp;gt;Any&amp;lt;/choice&amp;gt;
       &amp;lt;search&amp;gt;
         &amp;lt;query&amp;gt;| tstats summariesonly=t count FROM datamodel=Web WHERE Web.site="$site$" "Web.eventtype"=pageview GROUPBY "Web.site","Web.http_session","Web.uri" 
 | stats last("Web.uri") AS landing_page by "Web.http_session" 
 | rex field=landing_page mode=sed "s/^(\/[^\/]+\/).*$/\1/"
 | top limit=20 landing_page 
 | eval landing_page_description=landing_page+" ("+count+")" 
 | fields landing_page landing_page_description&amp;lt;/query&amp;gt;
         &amp;lt;earliest&amp;gt;0&amp;lt;/earliest&amp;gt;
         &amp;lt;latest&amp;gt;now&amp;lt;/latest&amp;gt;
       &amp;lt;/search&amp;gt;
       &amp;lt;fieldForLabel&amp;gt;landing_page_description&amp;lt;/fieldForLabel&amp;gt;
       &amp;lt;fieldForValue&amp;gt;landing_page&amp;lt;/fieldForValue&amp;gt;
       &amp;lt;default&amp;gt;*&amp;lt;/default&amp;gt;
       &amp;lt;change&amp;gt;
            &amp;lt;eval token="rex_landing_page"&amp;gt;if($value$=="*", "s/^(\\/[^\\/]+\\/).*$/\\1/", ".*")&amp;lt;/eval&amp;gt;
       &amp;lt;/change&amp;gt;
     &amp;lt;/input&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;PS: In your query 3rd line you are having a typo with variable name as rex_langing_page. Even if you correct this type you can use it as token in subsequent query (you might have to check out documentation on &lt;STRONG&gt;map&lt;/STRONG&gt; command in Splunk if you want to set the token within a query being run.)&lt;/P&gt;

&lt;HR /&gt;

&lt;P&gt;Updated answer all forward slashes need to be escaped in eval tag: &lt;CODE&gt;s/^(\\/[^\\/]+\\/).*$/\\1/&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 14:19:35 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337003#M100007</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2020-09-29T14:19:35Z</dc:date>
    </item>
    <item>
      <title>Re: Pass a variable to a rex command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337004#M100008</link>
      <description>&lt;P&gt;I'm getting an error:   Error in 'rex' command: Regex: missing terminating ] for character class &lt;/P&gt;</description>
      <pubDate>Tue, 06 Jun 2017 08:57:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337004#M100008</guid>
      <dc:creator>JohannesGmelin</dc:creator>
      <dc:date>2017-06-06T08:57:45Z</dc:date>
    </item>
    <item>
      <title>Re: Pass a variable to a rex command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337005#M100009</link>
      <description>&lt;P&gt;Can you print $rex_landing_page$ in your dashboard to check whether the rex is getting escaped?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;row&amp;gt;
  &amp;lt;panel&amp;gt;
    &amp;lt;html&amp;gt;
      rex_landing_page: $rex_landing_page$
    &amp;lt;/html&amp;gt;
  &amp;lt;/panel&amp;gt;
&amp;lt;/row&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Also I hope you have already validated regular expressions by hard-coding in your query.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 14:23:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337005#M100009</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2020-09-29T14:23:05Z</dc:date>
    </item>
    <item>
      <title>Re: Pass a variable to a rex command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337006#M100010</link>
      <description>&lt;P&gt;Let me convert this to comment, until this is resolved. Specially for others to provide their solutions/workaround.&lt;/P&gt;</description>
      <pubDate>Tue, 06 Jun 2017 10:21:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337006#M100010</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-06-06T10:21:51Z</dc:date>
    </item>
    <item>
      <title>Re: Pass a variable to a rex command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337007#M100011</link>
      <description>&lt;P&gt;This is the result:&lt;/P&gt;

&lt;P&gt;rex_landing_page: s/^([^]+).*$//&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 14:23:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337007#M100011</guid>
      <dc:creator>JohannesGmelin</dc:creator>
      <dc:date>2020-09-29T14:23:33Z</dc:date>
    </item>
    <item>
      <title>Re: Pass a variable to a rex command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337008#M100012</link>
      <description>&lt;P&gt;@JohannesGmelin, slashes need to be escaped in eval. I have updated my comment. Please try with the following eval condition:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;&amp;lt;eval token="rex_landing_page"&amp;gt;if($value$=="*", "s/^(\\/[^\\/]+\\/).*$/\\1/", ".*")&amp;lt;/eval&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 07 Jun 2017 09:07:52 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337008#M100012</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-06-07T09:07:52Z</dc:date>
    </item>
    <item>
      <title>Re: Pass a variable to a rex command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337009#M100013</link>
      <description>&lt;P&gt;@JohannesGmelin, hope this worked for you. Let me convert this to answer so that you can accept and mark your question as answered.&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 09:09:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337009#M100013</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-06-07T09:09:48Z</dc:date>
    </item>
    <item>
      <title>Re: Pass a variable to a rex command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337010#M100014</link>
      <description>&lt;P&gt;Oh great it's working. But the second command "replace with nothing (don't change something)" don't work too:&lt;/P&gt;

&lt;P&gt;Error in 'rex' command: Failed to initialize sed. cannot find sed command: . &lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 09:14:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337010#M100014</guid>
      <dc:creator>JohannesGmelin</dc:creator>
      <dc:date>2017-06-07T09:14:21Z</dc:date>
    </item>
    <item>
      <title>Re: Pass a variable to a rex command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337011#M100015</link>
      <description>&lt;P&gt;When you pass static values to your rex field does it work? &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| rex field=Web.uri mode=sed ".*"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;As per your question this is the other value that the eval expression will set&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 10:12:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337011#M100015</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-06-07T10:12:42Z</dc:date>
    </item>
    <item>
      <title>Re: Pass a variable to a rex command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337012#M100016</link>
      <description>&lt;PRE&gt;&lt;CODE&gt;&amp;lt;eval token="rex_landing_page"&amp;gt;if($value$=="*", "s/^(\\/[^\\/]+\\/).*$/\\1/", "s/(.*)$/\\1/")&amp;lt;/eval&amp;gt;
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This is the solution. Thanks for your help! &lt;/P&gt;

&lt;P&gt;Bye&lt;BR /&gt;
Johannes&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 10:24:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337012#M100016</guid>
      <dc:creator>JohannesGmelin</dc:creator>
      <dc:date>2017-06-07T10:24:34Z</dc:date>
    </item>
    <item>
      <title>Re: Pass a variable to a rex command</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337013#M100017</link>
      <description>&lt;P&gt;Great that it worked &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 07 Jun 2017 12:27:57 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Pass-a-variable-to-a-rex-command/m-p/337013#M100017</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-06-07T12:27:57Z</dc:date>
    </item>
  </channel>
</rss>

