<?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 How do I select first and second match as separate fields using Rex? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-select-first-and-second-match-as-separate-fields-using/m-p/396033#M114985</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;

&lt;P&gt;I have 1 field in Splunk which contains 2 short email headers in plain-text, for example:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; **From**: Me (me@me.com)
 **Sent**: 28 September 2018 17:42
 **To**: You (you@you.com)
 **Subject**: This is the first email

 **From**: Me (me@me.com)
 **Sent**: 28 September 2018 18:42
 **To**: You-aswell (you-aswell@you.com)
 **Subject**: This is the second email

 There is more text after the 2 short email headers.
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I would like to use Rex to select the 2 Sent times, i.e:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;rex field=output "Sent: (?&amp;lt;sent_time_1&amp;gt;.*)"

rex field=output "Sent: (?&amp;lt;sent_time_2&amp;gt;.*)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;How do I select in the rex function which match to select? As an FYI, there may be text before the headers so selecting the line number wouldn't be an option.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;/P&gt;</description>
    <pubDate>Mon, 01 Oct 2018 09:45:12 GMT</pubDate>
    <dc:creator>aherrington</dc:creator>
    <dc:date>2018-10-01T09:45:12Z</dc:date>
    <item>
      <title>How do I select first and second match as separate fields using Rex?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-select-first-and-second-match-as-separate-fields-using/m-p/396033#M114985</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;

&lt;P&gt;I have 1 field in Splunk which contains 2 short email headers in plain-text, for example:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; **From**: Me (me@me.com)
 **Sent**: 28 September 2018 17:42
 **To**: You (you@you.com)
 **Subject**: This is the first email

 **From**: Me (me@me.com)
 **Sent**: 28 September 2018 18:42
 **To**: You-aswell (you-aswell@you.com)
 **Subject**: This is the second email

 There is more text after the 2 short email headers.
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I would like to use Rex to select the 2 Sent times, i.e:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;rex field=output "Sent: (?&amp;lt;sent_time_1&amp;gt;.*)"

rex field=output "Sent: (?&amp;lt;sent_time_2&amp;gt;.*)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;How do I select in the rex function which match to select? As an FYI, there may be text before the headers so selecting the line number wouldn't be an option.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Mon, 01 Oct 2018 09:45:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-select-first-and-second-match-as-separate-fields-using/m-p/396033#M114985</guid>
      <dc:creator>aherrington</dc:creator>
      <dc:date>2018-10-01T09:45:12Z</dc:date>
    </item>
    <item>
      <title>Re: How do I select first and second match as separate fields using Rex?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-select-first-and-second-match-as-separate-fields-using/m-p/396034#M114986</link>
      <description>&lt;P&gt;why not just use something like this?&lt;BR /&gt;
    | rex field=output "Sent: (?&lt;SENT_TIME&gt;.*)" max_match=0&lt;BR /&gt;
This will give you both the matches in a multivalue value, if you need them as separate rows just append |mvexpand sent_time after the max_match=0&lt;/SENT_TIME&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 21:24:58 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-select-first-and-second-match-as-separate-fields-using/m-p/396034#M114986</guid>
      <dc:creator>Sukisen1981</dc:creator>
      <dc:date>2020-09-29T21:24:58Z</dc:date>
    </item>
    <item>
      <title>Re: How do I select first and second match as separate fields using Rex?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-select-first-and-second-match-as-separate-fields-using/m-p/396035#M114987</link>
      <description>&lt;P&gt;Thanks for your answer, that works to get the result on different rows, however I need the result to be in separate fields.&lt;/P&gt;

&lt;P&gt;Can this method be adapted for that?&lt;/P&gt;

&lt;P&gt;Thanks,&lt;/P&gt;</description>
      <pubDate>Mon, 01 Oct 2018 12:18:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-select-first-and-second-match-as-separate-fields-using/m-p/396035#M114987</guid>
      <dc:creator>aherrington</dc:creator>
      <dc:date>2018-10-01T12:18:08Z</dc:date>
    </item>
    <item>
      <title>Re: How do I select first and second match as separate fields using Rex?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-select-first-and-second-match-as-separate-fields-using/m-p/396036#M114988</link>
      <description>&lt;P&gt;If it's always two, you could do this..., &lt;/P&gt;

&lt;P&gt;| makeresults | eval output="&lt;STRONG&gt;From&lt;/STRONG&gt;: Me (&lt;A href="mailto:me@me.com" target="_blank"&gt;me@me.com&lt;/A&gt;)&lt;BR /&gt;
  &lt;STRONG&gt;Sent&lt;/STRONG&gt;: 28 September 2018 17:42&lt;BR /&gt;
  &lt;STRONG&gt;To&lt;/STRONG&gt;: You (&lt;A href="mailto:you@you.com" target="_blank"&gt;you@you.com&lt;/A&gt;)&lt;BR /&gt;
  &lt;STRONG&gt;Subject&lt;/STRONG&gt;: This is the first email&lt;/P&gt;

&lt;P&gt;&lt;STRONG&gt;From&lt;/STRONG&gt;: Me (&lt;A href="mailto:me@me.com" target="_blank"&gt;me@me.com&lt;/A&gt;)&lt;BR /&gt;
  &lt;STRONG&gt;Sent&lt;/STRONG&gt;: 28 September 2018 18:42&lt;BR /&gt;
  &lt;STRONG&gt;To&lt;/STRONG&gt;: You-aswell (&lt;A href="mailto:you-aswell@you.com" target="_blank"&gt;you-aswell@you.com&lt;/A&gt;)&lt;BR /&gt;
  &lt;STRONG&gt;Subject&lt;/STRONG&gt;: This is the second email&lt;BR /&gt;
  There is more stuff after the email headers, so this here represents more stuff"&lt;BR /&gt;
| rex field=output "**Sent**:\s(?\d+\s\w+\s\d+\s\d+:\d+)" max_match=0&lt;BR /&gt;
| eval sent_time_1=mvindex(sent_time, 0) &lt;BR /&gt;
| eval sent_time_2=mvindex(sent_time, 1)&lt;BR /&gt;
| table output sent_time_1, sent_time_2&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 21:25:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-select-first-and-second-match-as-separate-fields-using/m-p/396036#M114988</guid>
      <dc:creator>darrenfuller</dc:creator>
      <dc:date>2020-09-29T21:25:01Z</dc:date>
    </item>
    <item>
      <title>Re: How do I select first and second match as separate fields using Rex?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-select-first-and-second-match-as-separate-fields-using/m-p/396037#M114989</link>
      <description>&lt;P&gt;That almost works, although I can't guarantee that there will always be 2, sometimes there will be 3 or 4. I need to make sure it only selects the first 2.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Oct 2018 15:15:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-select-first-and-second-match-as-separate-fields-using/m-p/396037#M114989</guid>
      <dc:creator>aherrington</dc:creator>
      <dc:date>2018-10-01T15:15:23Z</dc:date>
    </item>
    <item>
      <title>Re: How do I select first and second match as separate fields using Rex?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-select-first-and-second-match-as-separate-fields-using/m-p/396038#M114990</link>
      <description>&lt;P&gt;Hi,&lt;BR /&gt;
mvindex will still work, ref documentation here - &lt;A href="http://docs.splunk.com/Documentation/Splunk/7.1.3/SearchReference/MultivalueEvalFunctions"&gt;http://docs.splunk.com/Documentation/Splunk/7.1.3/SearchReference/MultivalueEvalFunctions&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;As the documentation says, mvindex starts from 0 and as @darrenfuller has stated, irrespective of 3-4..n matches in your output field for the above rex, mvindex of 0 and 1 are going to fetch the 1st and 2nd fields respectively.&lt;BR /&gt;
Can you please try and confirm?&lt;/P&gt;</description>
      <pubDate>Mon, 01 Oct 2018 16:35:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-select-first-and-second-match-as-separate-fields-using/m-p/396038#M114990</guid>
      <dc:creator>Sukisen1981</dc:creator>
      <dc:date>2018-10-01T16:35:47Z</dc:date>
    </item>
    <item>
      <title>Re: How do I select first and second match as separate fields using Rex?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-select-first-and-second-match-as-separate-fields-using/m-p/396039#M114991</link>
      <description>&lt;P&gt;Hello, apologies it was an error from a different function that threw up the error. This worked perfectly, thank you for your help&lt;/P&gt;</description>
      <pubDate>Tue, 02 Oct 2018 09:49:26 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-select-first-and-second-match-as-separate-fields-using/m-p/396039#M114991</guid>
      <dc:creator>aherrington</dc:creator>
      <dc:date>2018-10-02T09:49:26Z</dc:date>
    </item>
    <item>
      <title>Re: How do I select first and second match as separate fields using Rex?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-do-I-select-first-and-second-match-as-separate-fields-using/m-p/597271#M207936</link>
      <description>&lt;P&gt;This method worked for me on an Windows Event log where "Account Name:" appears twice on every event and I only wanted the 2nd name, not the first. The only oddity unresolved was when one of the two names were null in the event. There might be a way to fix that. Other than that, it worked.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;index=* host=*servername* password EventCode=4625&lt;/P&gt;&lt;P&gt;| rex field=Message "Account Name:\s+(?&amp;lt;ACCOUNT_NAME&amp;gt;[^\r\n]+)" max_match=0&lt;/P&gt;&lt;P&gt;| eval Account_Name_1=mvindex(Account_Name, 0)&lt;/P&gt;&lt;P&gt;| eval Account_Name_2=mvindex(Account_Name, 1)&lt;/P&gt;&lt;P&gt;| table DomainController, Account_Name_2&lt;/P&gt;</description>
      <pubDate>Wed, 11 May 2022 01:02:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-do-I-select-first-and-second-match-as-separate-fields-using/m-p/597271#M207936</guid>
      <dc:creator>RedStapler</dc:creator>
      <dc:date>2022-05-11T01:02:31Z</dc:date>
    </item>
  </channel>
</rss>

