<?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: Iterate over lookup table to perform replace on search field in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Iterate-over-lookup-table-to-perform-replace-on-search-field/m-p/553598#M157180</link>
    <description>&lt;P&gt;If I'm going to do that I might as well just hard-code the search/replace strings in a series of eval replace commands, rather than do it as a lookup table.&lt;BR /&gt;&lt;BR /&gt;My idea here is that I want the search/replace iterations to be dynamic based on the contents of the lookup table.&amp;nbsp; I expect the table will grow over time.&lt;BR /&gt;&lt;BR /&gt;If Splunk can't do this (or at least without some major hacky work-arounds) then the hard-coding might just be the way to go, even though it is far from an ideal approach... I'm fairly new to Splunk, so maybe there is something I'm missing here?&lt;/P&gt;</description>
    <pubDate>Fri, 28 May 2021 22:42:51 GMT</pubDate>
    <dc:creator>wanderson8</dc:creator>
    <dc:date>2021-05-28T22:42:51Z</dc:date>
    <item>
      <title>Iterate over lookup table to perform replace on search field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Iterate-over-lookup-table-to-perform-replace-on-search-field/m-p/553575#M157168</link>
      <description>&lt;P&gt;I am trying to use a lookup table to perform a series of string replacements on a single field in a search result&lt;BR /&gt;&lt;BR /&gt;The lookup table has two fields:&lt;BR /&gt;&lt;BR /&gt;find_string, replace_string&lt;BR /&gt;&lt;BR /&gt;(??? find_string may need to be a regex for this purpose ???)&lt;BR /&gt;&lt;BR /&gt;Then, for every row/event in the search result, I need it to iterate over the lookup table and perform the following operation for a single field from the search results (call it search_field) :&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| eval search_field = replace(search_field, find_string, replace_string)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;The search_field mutations should be cumulative within each search row/event.&amp;nbsp; In other words, the return value from the replace function will become the input to the next iteration, until every entry in the lookup table has been iterated over.&amp;nbsp; Then it moves onto the next row/event and starts over with the original value of search_field at the start of the lookup table, ETC...&lt;BR /&gt;&lt;BR /&gt;I have tried many different approaches to this, with no success.&amp;nbsp; Apparently unlike SQL, subsearches in Splunk are unable to access fields from the outer search.&amp;nbsp; (???)&amp;nbsp; I have also had no success with the map command.&amp;nbsp; This seems like a fairly basic operation in most programming languages, and I think it would be even be do-able in SQL.&amp;nbsp; Is it even possible to do this with Splunk?&lt;/P&gt;</description>
      <pubDate>Fri, 28 May 2021 19:00:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Iterate-over-lookup-table-to-perform-replace-on-search-field/m-p/553575#M157168</guid>
      <dc:creator>wanderson8</dc:creator>
      <dc:date>2021-05-28T19:00:21Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate over lookup table to perform replace on search field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Iterate-over-lookup-table-to-perform-replace-on-search-field/m-p/553589#M157173</link>
      <description>&lt;P&gt;It may be possible depending on how many rows you have in you lookup table. Essentially, one approach might be to create a field for each row then use foreach to iterate over the fields.&lt;/P&gt;</description>
      <pubDate>Fri, 28 May 2021 21:10:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Iterate-over-lookup-table-to-perform-replace-on-search-field/m-p/553589#M157173</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2021-05-28T21:10:09Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate over lookup table to perform replace on search field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Iterate-over-lookup-table-to-perform-replace-on-search-field/m-p/553598#M157180</link>
      <description>&lt;P&gt;If I'm going to do that I might as well just hard-code the search/replace strings in a series of eval replace commands, rather than do it as a lookup table.&lt;BR /&gt;&lt;BR /&gt;My idea here is that I want the search/replace iterations to be dynamic based on the contents of the lookup table.&amp;nbsp; I expect the table will grow over time.&lt;BR /&gt;&lt;BR /&gt;If Splunk can't do this (or at least without some major hacky work-arounds) then the hard-coding might just be the way to go, even though it is far from an ideal approach... I'm fairly new to Splunk, so maybe there is something I'm missing here?&lt;/P&gt;</description>
      <pubDate>Fri, 28 May 2021 22:42:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Iterate-over-lookup-table-to-perform-replace-on-search-field/m-p/553598#M157180</guid>
      <dc:creator>wanderson8</dc:creator>
      <dc:date>2021-05-28T22:42:51Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate over lookup table to perform replace on search field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Iterate-over-lookup-table-to-perform-replace-on-search-field/m-p/553608#M157185</link>
      <description>&lt;P&gt;Fair enough, I wasn't clear when I said create a field, I meant dynamically create a field (from the rows in the lookup table)&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;| makeresults count=10
| streamstats count as row 
| eval _raw="data for user".mvindex(split("ABCDEFGHIJ",""),(row-1)%10)." is ".(row*123)
``` creates dummy data ```

| append
    [| makeresults count=5
    | fields - _time
    | streamstats count as user
    | eval match="user".mvindex(split("ABCDEFGHIJ",""),(10-user)%10)." is \\d+"
    | eval replacement="user".mvindex(split("ABCDEFGHIJ",""),(10-user)%10)." is XXX"
``` creates lookup data ```

``` count the rows in the lookup table ```
    | streamstats count as row
``` convert to names for columns ```
    | eval column="replace_".row
``` create mvfield with match and replacement strings ```
    | eval concat=mvappend(match,replacement)
``` reduce fields to name and mvfield ```
    | fields column concat
``` transpose rows to columns ```
    | transpose 0 header_field=column
``` drop column field ```
    | fields - column]
``` reverse events so that appended event is at the top ```
| reverse
``` copy replacement strings to all rows ```
| filldown replace_*
``` ignore appended row ```
| where isnotnull(_raw)
``` restore order of events ```
| reverse
``` copy the field being changed - not necessary, merely to show the change ```
| eval original=_raw
``` for each replacement ```
| foreach replace_*
``` perform the replacement on the field ```
    [| eval _raw=replace(_raw,mvindex(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,0),mvindex(&amp;lt;&amp;lt;FIELD&amp;gt;&amp;gt;,1))]&lt;/LI-CODE&gt;</description>
      <pubDate>Sat, 29 May 2021 06:15:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Iterate-over-lookup-table-to-perform-replace-on-search-field/m-p/553608#M157185</guid>
      <dc:creator>ITWhisperer</dc:creator>
      <dc:date>2021-05-29T06:15:34Z</dc:date>
    </item>
    <item>
      <title>Re: Iterate over lookup table to perform replace on search field</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Iterate-over-lookup-table-to-perform-replace-on-search-field/m-p/553862#M157245</link>
      <description>&lt;P&gt;THANK YOU SO MUCH.&amp;nbsp; This works!&lt;BR /&gt;&lt;BR /&gt;It seems a little hacky and inefficient, but that's more a limitation of the Splunk query language than anything!&amp;nbsp; Only wish there was a more efficient and sensible way to do this &lt;span class="lia-unicode-emoji" title=":confused_face:"&gt;😕&lt;/span&gt; ... guess I could start looking at making custom apps!&lt;/P&gt;</description>
      <pubDate>Tue, 01 Jun 2021 15:17:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Iterate-over-lookup-table-to-perform-replace-on-search-field/m-p/553862#M157245</guid>
      <dc:creator>wanderson8</dc:creator>
      <dc:date>2021-06-01T15:17:08Z</dc:date>
    </item>
  </channel>
</rss>

