<?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: Search query to replace first occurrence word with blank but second occurrence to replace with comma in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Search-query-to-replace-first-occurrence-word-with-blank-but/m-p/300500#M165757</link>
    <description>&lt;P&gt;Hi @cpetterborg, great rex command... Great learning !&lt;/P&gt;

&lt;P&gt;to other rex beginners, let me explain it - &lt;BR /&gt;
"s/^(\S+)(.&lt;EM&gt;?)\s(\1)/\2, /"&lt;BR /&gt;
&lt;CODE&gt;^(\S+)&lt;/CODE&gt; --- captures the first word&lt;BR /&gt;
`(.&lt;/EM&gt;?)&lt;CODE&gt;------ remaining line is captured as "\2", till the 2nd ubuntu match&lt;BR /&gt;
&lt;/CODE&gt;\s(\1)&lt;CODE&gt;---- matching for "a space and word ubuntu"&lt;BR /&gt;
before the "/", only matching part, after this "/", its the replacement part&lt;BR /&gt;
&lt;/CODE&gt;\2,&lt;CODE&gt;--- on the replacement, leave the&lt;/CODE&gt;\1`, write the "\2" match and then a comma ",". thats it. &lt;/P&gt;</description>
    <pubDate>Mon, 09 Oct 2017 06:41:32 GMT</pubDate>
    <dc:creator>inventsekar</dc:creator>
    <dc:date>2017-10-09T06:41:32Z</dc:date>
    <item>
      <title>Search query to replace first occurrence word with blank but second occurrence to replace with comma</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-query-to-replace-first-occurrence-word-with-blank-but/m-p/300497#M165754</link>
      <description>&lt;P&gt;How do I use regex or replace to remove the first occurrence word found and replace second occurrence onward with comma?&lt;/P&gt;

&lt;P&gt;For example, the raw data is:&lt;BR /&gt;
ubuntu CRON[2907]: pam_unix(cron:session): session opened for user root by (uid=0) ubuntu CRON[2907]: pam_unix(cron:session): session closed for user root &lt;/P&gt;

&lt;P&gt;I want it to be:&lt;BR /&gt;
CRON[2907]: pam_unix(cron:session): session opened for user root by (uid=0),CRON[2907]: pam_unix(cron:session): session closed for user root &lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 16:12:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-query-to-replace-first-occurrence-word-with-blank-but/m-p/300497#M165754</guid>
      <dc:creator>Kitteh</dc:creator>
      <dc:date>2020-09-29T16:12:32Z</dc:date>
    </item>
    <item>
      <title>Re: Search query to replace first occurrence word with blank but second occurrence to replace with comma</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-query-to-replace-first-occurrence-word-with-blank-but/m-p/300498#M165755</link>
      <description>&lt;P&gt;If you have only one second occurrence of the beginning string, this will work:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval _raw="ubuntu CRON[2907]: pam_unix(cron:session): session opened for user root by (uid=0) ubuntu CRON[2907]: pam_unix(cron:session): session closed for user root by (uid=0)" 
| rex mode=sed "s/^(\S+)(.*?)\s(\1)/\2, /"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The process for multiple occurrences is more complex. Is the data in that case similar to the example that you provided? if not can you provide an example? Is there a maximum number of occurrences?&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2017 06:03:33 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-query-to-replace-first-occurrence-word-with-blank-but/m-p/300498#M165755</guid>
      <dc:creator>cpetterborg</dc:creator>
      <dc:date>2017-10-09T06:03:33Z</dc:date>
    </item>
    <item>
      <title>Re: Search query to replace first occurrence word with blank but second occurrence to replace with comma</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-query-to-replace-first-occurrence-word-with-blank-but/m-p/300499#M165756</link>
      <description>&lt;P&gt;You can run rex two times, first time to replace the first ubuntu with blank,&lt;BR /&gt;
second ubuntu with a comma&lt;/P&gt;

&lt;P&gt;(if the string "ubuntu" is not known before hand, please update some more details(which spot it appears), so that rex can be updated) &lt;BR /&gt;
(rex mode=sed can not be tested on regex101 website, i have tested it on splunk directly, it works fine.. please check the screenshot)&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|makeresults
 | eval _raw = "ubuntu CRON[2907]: pam_unix(cron:session): session opened for user root by (uid=0) ubuntu CRON[2907]: pam_unix(cron:session): session closed for user root"
 | rex mode=sed field=_raw "s#(^ubuntu\s)##"
 | rex mode=sed field=_raw "s#ubuntu#,#"
 | table _raw
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="alt text"&gt;&lt;img src="https://community.splunk.com/t5/image/serverpage/image-id/3640i8EDB263F7A5AD94F/image-size/large?v=v2&amp;amp;px=999" role="button" title="alt text" alt="alt text" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2017 06:11:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-query-to-replace-first-occurrence-word-with-blank-but/m-p/300499#M165756</guid>
      <dc:creator>inventsekar</dc:creator>
      <dc:date>2017-10-09T06:11:06Z</dc:date>
    </item>
    <item>
      <title>Re: Search query to replace first occurrence word with blank but second occurrence to replace with comma</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-query-to-replace-first-occurrence-word-with-blank-but/m-p/300500#M165757</link>
      <description>&lt;P&gt;Hi @cpetterborg, great rex command... Great learning !&lt;/P&gt;

&lt;P&gt;to other rex beginners, let me explain it - &lt;BR /&gt;
"s/^(\S+)(.&lt;EM&gt;?)\s(\1)/\2, /"&lt;BR /&gt;
&lt;CODE&gt;^(\S+)&lt;/CODE&gt; --- captures the first word&lt;BR /&gt;
`(.&lt;/EM&gt;?)&lt;CODE&gt;------ remaining line is captured as "\2", till the 2nd ubuntu match&lt;BR /&gt;
&lt;/CODE&gt;\s(\1)&lt;CODE&gt;---- matching for "a space and word ubuntu"&lt;BR /&gt;
before the "/", only matching part, after this "/", its the replacement part&lt;BR /&gt;
&lt;/CODE&gt;\2,&lt;CODE&gt;--- on the replacement, leave the&lt;/CODE&gt;\1`, write the "\2" match and then a comma ",". thats it. &lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2017 06:41:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-query-to-replace-first-occurrence-word-with-blank-but/m-p/300500#M165757</guid>
      <dc:creator>inventsekar</dc:creator>
      <dc:date>2017-10-09T06:41:32Z</dc:date>
    </item>
    <item>
      <title>Re: Search query to replace first occurrence word with blank but second occurrence to replace with comma</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Search-query-to-replace-first-occurrence-word-with-blank-but/m-p/300501#M165758</link>
      <description>&lt;P&gt;Thank you. I saw your original post in email. I'm glad you figured it all out. Congratulations! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; I've upvoted your comment for the fine explanation!&lt;/P&gt;</description>
      <pubDate>Mon, 09 Oct 2017 18:15:41 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Search-query-to-replace-first-occurrence-word-with-blank-but/m-p/300501#M165758</guid>
      <dc:creator>cpetterborg</dc:creator>
      <dc:date>2017-10-09T18:15:41Z</dc:date>
    </item>
  </channel>
</rss>

