<?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 Using Rex Command to Extract Username from EventData_Xml Data in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354033#M165382</link>
    <description>&lt;P&gt;Good morning.  I'm trying to use rex to extract a username from a MS Windows Application Event Log.  The event shows a field called "EventData_Xml" and in there is the following (NOTE: I replaced greater than signs with brackets as it was treating it as HTML and not displaying properly):&lt;/P&gt;

&lt;P&gt;[Data]kjewgjkewkj[/Data][Data] Reason: Could not find a login matching the name provided.[/Data][Data] [CLIENT: &amp;lt;local machine&amp;gt;][/Data][Binary]blah, blah, blah[/binary]"&lt;/P&gt;

&lt;P&gt;The username is showing in between the first  tags (in this case kjewgjkewkj).  I put in a fake username and tried connecting to create a failed login event for MSSQL.  So I was trying to use rex to grab the text between the first two data tags but I can't get it to work.  Using the field extractor in Splunk seems overly complicated when looking at the search code it produces as it is using the _raw field.  Is there a simpler way to do it using something like the following:&lt;/P&gt;

&lt;P&gt;host=blah* source="WinEventLog:Application"|xmlkv|search EventID=18456|rex field=EventData_Xml "(?)"&lt;/P&gt;

&lt;P&gt;Thanks for any help.&lt;/P&gt;</description>
    <pubDate>Tue, 07 Nov 2017 14:14:11 GMT</pubDate>
    <dc:creator>SplunkLunk</dc:creator>
    <dc:date>2017-11-07T14:14:11Z</dc:date>
    <item>
      <title>Using Rex Command to Extract Username from EventData_Xml Data</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354033#M165382</link>
      <description>&lt;P&gt;Good morning.  I'm trying to use rex to extract a username from a MS Windows Application Event Log.  The event shows a field called "EventData_Xml" and in there is the following (NOTE: I replaced greater than signs with brackets as it was treating it as HTML and not displaying properly):&lt;/P&gt;

&lt;P&gt;[Data]kjewgjkewkj[/Data][Data] Reason: Could not find a login matching the name provided.[/Data][Data] [CLIENT: &amp;lt;local machine&amp;gt;][/Data][Binary]blah, blah, blah[/binary]"&lt;/P&gt;

&lt;P&gt;The username is showing in between the first  tags (in this case kjewgjkewkj).  I put in a fake username and tried connecting to create a failed login event for MSSQL.  So I was trying to use rex to grab the text between the first two data tags but I can't get it to work.  Using the field extractor in Splunk seems overly complicated when looking at the search code it produces as it is using the _raw field.  Is there a simpler way to do it using something like the following:&lt;/P&gt;

&lt;P&gt;host=blah* source="WinEventLog:Application"|xmlkv|search EventID=18456|rex field=EventData_Xml "(?)"&lt;/P&gt;

&lt;P&gt;Thanks for any help.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Nov 2017 14:14:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354033#M165382</guid>
      <dc:creator>SplunkLunk</dc:creator>
      <dc:date>2017-11-07T14:14:11Z</dc:date>
    </item>
    <item>
      <title>Re: Using Rex Command to Extract Username from EventData_Xml Data</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354034#M165383</link>
      <description>&lt;P&gt;Hi SplunkLunk,&lt;BR /&gt;
Di you tried this rex&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;host=blah* source="WinEventLog:Application"
|xmlkv
|search EventID=18456
|rex field=EventData_Xml "\[Data\](?&amp;lt;UserName&amp;gt;[^\[]*)\[\/Data\]"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You can test it at &lt;A href="https://regex101.com/r/EdaRpI/1"&gt;https://regex101.com/r/EdaRpI/1&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Bye.&lt;BR /&gt;
Giuseppe&lt;/P&gt;</description>
      <pubDate>Tue, 07 Nov 2017 14:21:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354034#M165383</guid>
      <dc:creator>gcusello</dc:creator>
      <dc:date>2017-11-07T14:21:36Z</dc:date>
    </item>
    <item>
      <title>Re: Using Rex Command to Extract Username from EventData_Xml Data</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354035#M165384</link>
      <description>&lt;P&gt;Thanks.  That's closer but it's adding the text from the next html data tag.  So now the username looks like:&lt;/P&gt;

&lt;P&gt;"kjewgjkewkj[/Data][Data] Reason: Could not find a login matching the name provided."&lt;/P&gt;

&lt;P&gt;Again, I've traded the greater than signs for brackets.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Nov 2017 14:32:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354035#M165384</guid>
      <dc:creator>SplunkLunk</dc:creator>
      <dc:date>2017-11-07T14:32:01Z</dc:date>
    </item>
    <item>
      <title>Re: Using Rex Command to Extract Username from EventData_Xml Data</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354036#M165385</link>
      <description>&lt;P&gt;@SplunkLunk, you should post code with &lt;CODE&gt;code&lt;/CODE&gt; button (one with &lt;CODE&gt;101010&lt;/CODE&gt;) so that it does not escape.&lt;/P&gt;

&lt;P&gt;If you are ingesting Windows Event logs in XML format, you can enable &lt;CODE&gt;KV_MODE=xml&lt;/CODE&gt; from &lt;CODE&gt;props.conf&lt;/CODE&gt;. Refer to documentation (&lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/Admin/Propsconf#Field_extraction_configuration"&gt;https://docs.splunk.com/Documentation/Splunk/latest/Admin/Propsconf#Field_extraction_configuration&lt;/A&gt;). This will generate search time field extraction from XML data.&lt;/P&gt;

&lt;P&gt;You can also use &lt;CODE&gt;spath&lt;/CODE&gt; command, to parse XML data. Since your _raw data is XML you can use &lt;CODE&gt;|  spath&lt;/CODE&gt; which should give you fields exctracted automatically. If you know complete structure of XML DOM, you can use &lt;CODE&gt;path&lt;/CODE&gt; in spath command to extract only the field you need. &lt;A href="https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Spath"&gt;https://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Spath&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Following is a run anywhere search using spath with the sample data provided, however, the structure &lt;CODE&gt;&amp;lt;CLIENT: &amp;lt;local machine&amp;gt;&amp;gt;&lt;/CODE&gt;, does not seem correct for XML:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|  makeresults
|  eval _raw="&amp;lt;Data&amp;gt;kjewgjkewkj&amp;lt;/Data&amp;gt;&amp;lt;Data&amp;gt; Reason: Could not find a login matching the name provided.&amp;lt;/Data&amp;gt;&amp;lt;Data&amp;gt;&amp;lt;CLIENT: &amp;lt;local machine&amp;gt;&amp;gt;&amp;lt;/Data&amp;gt;&amp;lt;Binary&amp;gt;blah, blah, blah&amp;lt;/binary&amp;gt;"
|  spath
|  eval UserName=mvindex(Data,0)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If you want to stick to rex command you can use the following:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|  makeresults
|  eval _raw="&amp;lt;Data&amp;gt;kjewgjkewkj&amp;lt;/Data&amp;gt;&amp;lt;Data&amp;gt; Reason: Could not find a login matching the name provided.&amp;lt;/Data&amp;gt;&amp;lt;Data&amp;gt;&amp;lt;CLIENT: &amp;lt;local machine&amp;gt;&amp;gt;&amp;lt;/Data&amp;gt;&amp;lt;Binary&amp;gt;blah, blah, blah&amp;lt;/binary&amp;gt;"
|  rex "\&amp;lt;Data\&amp;gt;(?&amp;lt;UserName&amp;gt;[^\&amp;lt;]+)\&amp;lt;\/Data\&amp;gt;"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;PS: By default &lt;CODE&gt;max_match&lt;/CODE&gt; parameter of rex command is set to &lt;CODE&gt;1&lt;/CODE&gt; which means it will only extract first occurrence of &lt;CODE&gt;&amp;lt;Data&amp;gt;&lt;/CODE&gt;.&lt;BR /&gt;
&lt;A href="http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Rex"&gt;http://docs.splunk.com/Documentation/Splunk/latest/SearchReference/Rex&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Since the exact answer depends on the structure of your data, please re-post sample data using code button (101010).&lt;/P&gt;</description>
      <pubDate>Tue, 07 Nov 2017 14:36:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354036#M165385</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-11-07T14:36:02Z</dc:date>
    </item>
    <item>
      <title>Re: Using Rex Command to Extract Username from EventData_Xml Data</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354037#M165386</link>
      <description>&lt;P&gt;Thanks.  I used the main part of your rex statement to make it work.  My search now looks like:&lt;/P&gt;

&lt;P&gt;host=blah* source="WinEventLog:Application"|xmlkv|search EventID=18456 OR EventID=18453 |rex field=EventData_Xml "[Data](?[User][^&amp;lt;]+)[\/Data]"&lt;/P&gt;

&lt;P&gt;This seems to get me the results I want (greater than signs switched for brackets).&lt;/P&gt;</description>
      <pubDate>Tue, 07 Nov 2017 14:50:32 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354037#M165386</guid>
      <dc:creator>SplunkLunk</dc:creator>
      <dc:date>2017-11-07T14:50:32Z</dc:date>
    </item>
    <item>
      <title>Re: Using Rex Command to Extract Username from EventData_Xml Data</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354038#M165387</link>
      <description>&lt;P&gt;Thanks.  The above poster had a subtle difference in the rex expression which stopped it at the first occurrence of the html tag.  That got me the results I wanted.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Nov 2017 14:52:43 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354038#M165387</guid>
      <dc:creator>SplunkLunk</dc:creator>
      <dc:date>2017-11-07T14:52:43Z</dc:date>
    </item>
    <item>
      <title>Re: Using Rex Command to Extract Username from EventData_Xml Data</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354039#M165388</link>
      <description>&lt;P&gt;add match=1 to the rex command&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; host=blah* source="WinEventLog:Application"
 |xmlkv
 |search EventID=18456
 |rex field=EventData_Xml "\[Data\](?&amp;lt;UserName&amp;gt;[^\[]*)\[\/Data\]" match=1
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Bye.&lt;BR /&gt;
Giuseppe&lt;/P&gt;</description>
      <pubDate>Tue, 07 Nov 2017 14:54:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354039#M165388</guid>
      <dc:creator>gcusello</dc:creator>
      <dc:date>2017-11-07T14:54:11Z</dc:date>
    </item>
    <item>
      <title>Re: Using Rex Command to Extract Username from EventData_Xml Data</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354040#M165389</link>
      <description>&lt;P&gt;If you're satisfied, please, accept or upvote it.&lt;BR /&gt;
Bye.&lt;BR /&gt;
Giuseppe&lt;/P&gt;</description>
      <pubDate>Tue, 07 Nov 2017 14:56:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354040#M165389</guid>
      <dc:creator>gcusello</dc:creator>
      <dc:date>2017-11-07T14:56:50Z</dc:date>
    </item>
    <item>
      <title>Re: Using Rex Command to Extract Username from EventData_Xml Data</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354041#M165390</link>
      <description>&lt;P&gt;adding max_match=1 still didn't seem to work.  The results for UserName still show:&lt;/P&gt;

&lt;P&gt;kjewgjkewkj[/Data][Data] Reason: Could not find a login matching the name provided.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Nov 2017 15:02:45 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354041#M165390</guid>
      <dc:creator>SplunkLunk</dc:creator>
      <dc:date>2017-11-07T15:02:45Z</dc:date>
    </item>
    <item>
      <title>Re: Using Rex Command to Extract Username from EventData_Xml Data</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354042#M165391</link>
      <description>&lt;P&gt;@SplunkLunk, I have converted my comment to Answer. Please accept.&lt;/P&gt;</description>
      <pubDate>Tue, 07 Nov 2017 15:04:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-Rex-Command-to-Extract-Username-from-EventData-Xml-Data/m-p/354042#M165391</guid>
      <dc:creator>niketn</dc:creator>
      <dc:date>2017-11-07T15:04:30Z</dc:date>
    </item>
  </channel>
</rss>

