Splunk Search

How to use rex in searchTemplate while creating forms

Anvita
Explorer

How to use rex in searchTemplate while form creation? When i try to use following search using rex, it gives me "Invalid xml" error.

<row>
    <single>
      <title>Total Logged in users across all states</title>      
      <searchTemplate> sourcetype=test1 "https://test1.com:999/t2/t3/*/home;sid=" | rex "https://test1.com:999/t2/t3/(?<statename>.*)/home" |  eval state=upper(statename) | lookup state_abbr state AS state OUTPUTNEW name AS name  | eval State=upper(name) | stats dc(sid) AS "Logged in Users"  by State </searchTemplate>
    </single>
</row>

statename is a dynamic variable I am creating which will be then applied on lookup to retrieve actual state name as data contains only the abbreviations of states(statename is abbreviation and name is actual state in lookup file). When I use it in search, it performs well but when used in form's searchTemplate, it throws an error.

Tags (1)
1 Solution

ziegfried
Influencer

You have to either escape the XML characters (<=&lt; >=&gt; &=&amp;) within you search:

  <searchTemplate> sourcetype=test1 "https://test1.com:999/t2/t3/*/home;sid=" | rex "https://test1.com:999/t2/t3/(?&lt;statename&gt;.*)/home" |  eval state=upper(statename) | lookup state_abbr state AS state OUTPUTNEW name AS name  | eval State=upper(name) | stats dc(sid) AS "Logged in Users"  by State </searchTemplate>

or put it into CDATA:

  <searchTemplate><![CDATA[ sourcetype=test1 "https://test1.com:999/t2/t3/*/home;sid=" | rex "https://test1.com:999/t2/t3/(?<statename>.*)/home" |  eval state=upper(statename) | lookup state_abbr state AS state OUTPUTNEW name AS name  | eval State=upper(name) | stats dc(sid) AS "Logged in Users"  by State ]]></searchTemplate>

View solution in original post

ziegfried
Influencer

You have to either escape the XML characters (<=&lt; >=&gt; &=&amp;) within you search:

  <searchTemplate> sourcetype=test1 "https://test1.com:999/t2/t3/*/home;sid=" | rex "https://test1.com:999/t2/t3/(?&lt;statename&gt;.*)/home" |  eval state=upper(statename) | lookup state_abbr state AS state OUTPUTNEW name AS name  | eval State=upper(name) | stats dc(sid) AS "Logged in Users"  by State </searchTemplate>

or put it into CDATA:

  <searchTemplate><![CDATA[ sourcetype=test1 "https://test1.com:999/t2/t3/*/home;sid=" | rex "https://test1.com:999/t2/t3/(?<statename>.*)/home" |  eval state=upper(statename) | lookup state_abbr state AS state OUTPUTNEW name AS name  | eval State=upper(name) | stats dc(sid) AS "Logged in Users"  by State ]]></searchTemplate>

Anvita
Explorer

Thank you so much ziegfried... It really works...

0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...