Splunk Search

How to escape all possible special characters

Thulasinathan_M
Communicator

Hi Splunk Experts,

I've a table and based on a click, I'm holding the value of field in token and using it in a different panel with search command. If there are any special characters the search is getting failed. I've tried replacing it with '*', but that gives me unexpected results. So I'm thinking of escaping all possible special characters in the token value. Please advice!!

Ex:

!@#$%^&*(){}|";:<>/\[]

I want them as below:

\!\@\#\$\%\^\&\*\(\)\{\}\|\"\;\:\<\>\/\\\[\]

 

Labels (2)
Tags (1)
0 Karma
1 Solution

ITWhisperer
SplunkTrust
SplunkTrust

Try like this

  <init>
    <set token="input">!@#$%^&amp;*(){}|\";:&lt;&gt;/\\[]</set>
  </init>
  <row>
    <panel depends="$alwayshide$">
      <html>
        <style>
          #escaped table tbody td div.multivalue-subcell[data-mv-index="1"] {
            display: none;
          }
        </style>
      </html>
    </panel>
    <panel id="escaped">
      <table>
        <title>$escaped$</title>
        <search>
          <query>| makeresults
| fields - _time
| eval param=$input|s$
| eval param=mvappend(param,replace(param,"([!@#$%^&amp;*\(\)\{\}\|\";:&lt;&gt;\/\\\[\]])","\\\\\1"))</query>
          <earliest>-24h@h</earliest>
          <latest>now</latest>
        </search>
        <option name="drilldown">cell</option>
        <option name="refresh.display">progressbar</option>
        <drilldown>
          <eval token="escaped">mvindex($click.value$,1)</eval>
        </drilldown>
      </table>
    </panel>
  </row>

View solution in original post

ITWhisperer
SplunkTrust
SplunkTrust

You could try replacing every special character with a backslash followed by that character.

In the following example, the token used in the title is set when the field is clicked. The token value is the contents of the second multi-value in the field, which has been hidden using CSS. (I tried using the replace in the token evaluation directly but it only seems to work on a field not a string.)

    <panel depends="$alwayshide$">
      <html>
        <style>
          #escaped table tbody td div.multivalue-subcell[data-mv-index="1"] {
            display: none;
          }
        </style>
      </html>
    </panel>
    <panel id="escaped">
      <table>
        <title>$escaped$</title>
        <search>
          <query>| makeresults
| fields - _time
| eval param="!@#$%^&amp;*(){}|\";:&lt;&gt;/\\[]"
| eval param=mvappend(param,replace(param,"([!@#$%^&amp;*\(\)\{\}\|\";:&lt;&gt;\/\\\[\]])","\\\\\1"))</query>
          <earliest>-24h@h</earliest>
          <latest>now</latest>
        </search>
        <option name="drilldown">cell</option>
        <option name="refresh.display">progressbar</option>
        <drilldown>
          <eval token="escaped">mvindex($click.value$,1)</eval>
        </drilldown>
      </table>
    </panel>

 

Thulasinathan_M
Communicator

Thanks @ITWhisperer. This is exactly what I'm looking for, but my text comes from a token. If I do something like below, I get " Error in 'SearchParser': Mismatched ']'."

<init>
    <eval token="input">"!@#$%^&amp;*(){}|\";:&lt;&gt;/\\[]"</eval>
  </init>
   <row>
   <panel depends="$alwayshide$">
      <html>
        <style>
          #escaped table tbody td div.multivalue-subcell[data-mv-index="1"] {
            display: none;
          }
        </style>
      </html>
    </panel>
    <panel id="escaped">
      <table>
        <title>$escaped$</title>
        <search>
          <query>| makeresults
| fields - _time
| eval param="$input$"
| eval param=mvappend(param,replace(param,"([!@#$%^&amp;*\(\)\{\}\|\";:&lt;&gt;\/\\\[\]])","\\\\\1"))</query>
          <earliest>-24h@h</earliest>
          <latest>now</latest>
        </search>
        <option name="drilldown">cell</option>
        <option name="refresh.display">progressbar</option>
        <drilldown>
          <eval token="escaped">mvindex($click.value$,1)</eval>
        </drilldown>
      </table>
    </panel>
     </row>

 

0 Karma

ITWhisperer
SplunkTrust
SplunkTrust

Try like this

  <init>
    <set token="input">!@#$%^&amp;*(){}|\";:&lt;&gt;/\\[]</set>
  </init>
  <row>
    <panel depends="$alwayshide$">
      <html>
        <style>
          #escaped table tbody td div.multivalue-subcell[data-mv-index="1"] {
            display: none;
          }
        </style>
      </html>
    </panel>
    <panel id="escaped">
      <table>
        <title>$escaped$</title>
        <search>
          <query>| makeresults
| fields - _time
| eval param=$input|s$
| eval param=mvappend(param,replace(param,"([!@#$%^&amp;*\(\)\{\}\|\";:&lt;&gt;\/\\\[\]])","\\\\\1"))</query>
          <earliest>-24h@h</earliest>
          <latest>now</latest>
        </search>
        <option name="drilldown">cell</option>
        <option name="refresh.display">progressbar</option>
        <drilldown>
          <eval token="escaped">mvindex($click.value$,1)</eval>
        </drilldown>
      </table>
    </panel>
  </row>

Thulasinathan_M
Communicator

Thanks, worked perfectly.

0 Karma

gcusello
SplunkTrust
SplunkTrust

Hi @Thulasinathan_M,

if you always have a special char in the beginning of the token, you could add a backslash "\" as prefix to the token.

Ciao.

Giuseppe

0 Karma

Thulasinathan_M
Communicator

Hi @gcusello, It can be anywhere in the text.

0 Karma
Get Updates on the Splunk Community!

Splunk APM: New Product Features + Community Office Hours Recap!

Howdy Splunk Community! Over the past few months, we’ve had a lot going on in the world of Splunk Application ...

Index This | Forward, I’m heavy; backward, I’m not. What am I?

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

A Guide To Cloud Migration Success

As enterprises’ rapid expansion to the cloud continues, IT leaders are continuously looking for ways to focus ...