<?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 regex to capture exactly 20 characters in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-capture-exactly-20-characters/m-p/34541#M7465</link>
    <description>&lt;P&gt;I need to create a field extraction that extracts the first 20 characters ONLY from an error log; I've got the regex that extracts the full error:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;rex "\#[\w0-9\W]{9}\:\s(?P!ERROR[^\\*]+)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;FYI in my regex above: !ERROR = &amp;lt; error&amp;gt; (no space) - the text editor is removing anything after &amp;lt; even when using the code sample optiion. &lt;/P&gt;

&lt;P&gt;Is there regex that will capture only the first 20 characters as the field &amp;lt; error&amp;gt;? Here are the logs in question and I provided an example of the field data I am trying to extract.&lt;/P&gt;

&lt;P&gt;I feel like I may be able to use the substr command for eval, but not exactly sure of the correct format... this doesn't seem to work:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;ex "\#[\w0-9\W]{9}\:\s(?P!ERROR[^\\*]+)" | top 100 error | eval error=substr("error", 1, 20)

s1-sn701:2012-08-14 09:55:09,723 INFO  [STDOUT] [ERROR] 2012-08-14 09:55:09           LP::ThisController - #aWMfOOXSL: EAL: ASYNC: in async payment, could not create items, api returned 320
s1-sn903:2012-08-14 07:01:34,169 INFO  [STDOUT] [ERROR] 2012-08-14 07:01:34           LP::OfferController - #dN'Fi&amp;lt;&amp;lt;Od: Error decoding or storing lat/long, exception was 'undefined method `[]' for nil:NilClass'
s1-sn902:2012-08-14 01:33:23,562 INFO  [STDOUT] [ERROR] 2012-08-14 01:33:23           UI::ReportController - #fm7e(n$2J: API returned 952 error for report data
s1-sn902:2012-08-14 01:11:31,431 INFO  [STDOUT] [ERROR] 2012-08-14 01:11:31           LP::ThisController - #9['?rp`fY: PAYKEY from payment data is blank or missing on item page
s1-sn902:2012-08-14 01:11:31,430 INFO  [STDOUT] [ERROR] 2012-08-14 01:11:31           LP::ThisController - #9['?rp`fY: PAYKEY from session is blank or missing on item page
s1-sn902:2012-08-14 00:15:16,746 INFO  [STDOUT] [ERROR] 2012-08-14 00:15:16           LP::ThisController - #Xq5Bez;vF: Attempting to purchase item that is expired
s1-sn701:2012-08-13 23:55:22,969 INFO  [STDOUT] [ERROR] 2012-08-13 23:55:22           LP::OfferController - #\)F3XjY_v: PAYKEY is blank or missing on item page
s1-sn701:2012-08-13 23:29:31,458 INFO  [STDOUT] [ERROR] 2012-08-13 23:29:31           LP::ThisController - #z|gXWQY1S: EAL: ASYNC: in async payment could not create items, api returned 320
s1-sn902:2012-08-13 12:40:13,350 INFO  [STDOUT] [ERROR] 2012-08-13 12:40:13           UI::Rails - #ErS;=x*'): Failed to get [1]https://aurl.url.com/v1/85/pp/accounting/  [2]betsy@betsyklein.com/
s1-sn902:2012-08-13 12:40:13,349 INFO  [STDOUT] [ERROR] 2012-08-13 12:40:13           UI::Rails - #ErS;=x*'): ["classpath:/META-INF/jruby.home/lib/ruby/1.8/uri/common.rb:436:in `split'"
s1-sn902:2012-08-13 12:40:13,347 INFO  [STDOUT] [ERROR] 2012-08-13 12:40:13           UI::Rails - #ErS;=x*'): -----------------------------
s1-sn902:2012-08-13 12:40:13,346 INFO  [STDOUT] [ERROR] 2012-08-13 12:40:13           UI::Rails - #ErS;=x*'): bad URI(is not URI?): [3]https://aurl.url.com/85/bills/pp/accounting/  [4]uname@aurl.com/
s1-sn902:2012-08-13 12:40:13,346 INFO  [STDOUT] [ERROR] 2012-08-13 12:40:13           UI::Rails - #ErS;=x*'): Oops, an error occured!
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Example of data I want to extract as the error field:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;EAL: ASYNC: in async
Error decoding or st
API returned 952 err
PAYKEY from payment 
PAYKEY from session 
Attempting to purcha
PAYKEY is blank or m
ASYNC: in async pay
Failed to get [1]htt
["classpath:/META-IN
--------------------
bad URI(is not URI?)
Oops, an error occur
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 14 Aug 2012 15:05:27 GMT</pubDate>
    <dc:creator>hharvey</dc:creator>
    <dc:date>2012-08-14T15:05:27Z</dc:date>
    <item>
      <title>Using regex to capture exactly 20 characters</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-capture-exactly-20-characters/m-p/34541#M7465</link>
      <description>&lt;P&gt;I need to create a field extraction that extracts the first 20 characters ONLY from an error log; I've got the regex that extracts the full error:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;rex "\#[\w0-9\W]{9}\:\s(?P!ERROR[^\\*]+)"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;FYI in my regex above: !ERROR = &amp;lt; error&amp;gt; (no space) - the text editor is removing anything after &amp;lt; even when using the code sample optiion. &lt;/P&gt;

&lt;P&gt;Is there regex that will capture only the first 20 characters as the field &amp;lt; error&amp;gt;? Here are the logs in question and I provided an example of the field data I am trying to extract.&lt;/P&gt;

&lt;P&gt;I feel like I may be able to use the substr command for eval, but not exactly sure of the correct format... this doesn't seem to work:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;ex "\#[\w0-9\W]{9}\:\s(?P!ERROR[^\\*]+)" | top 100 error | eval error=substr("error", 1, 20)

s1-sn701:2012-08-14 09:55:09,723 INFO  [STDOUT] [ERROR] 2012-08-14 09:55:09           LP::ThisController - #aWMfOOXSL: EAL: ASYNC: in async payment, could not create items, api returned 320
s1-sn903:2012-08-14 07:01:34,169 INFO  [STDOUT] [ERROR] 2012-08-14 07:01:34           LP::OfferController - #dN'Fi&amp;lt;&amp;lt;Od: Error decoding or storing lat/long, exception was 'undefined method `[]' for nil:NilClass'
s1-sn902:2012-08-14 01:33:23,562 INFO  [STDOUT] [ERROR] 2012-08-14 01:33:23           UI::ReportController - #fm7e(n$2J: API returned 952 error for report data
s1-sn902:2012-08-14 01:11:31,431 INFO  [STDOUT] [ERROR] 2012-08-14 01:11:31           LP::ThisController - #9['?rp`fY: PAYKEY from payment data is blank or missing on item page
s1-sn902:2012-08-14 01:11:31,430 INFO  [STDOUT] [ERROR] 2012-08-14 01:11:31           LP::ThisController - #9['?rp`fY: PAYKEY from session is blank or missing on item page
s1-sn902:2012-08-14 00:15:16,746 INFO  [STDOUT] [ERROR] 2012-08-14 00:15:16           LP::ThisController - #Xq5Bez;vF: Attempting to purchase item that is expired
s1-sn701:2012-08-13 23:55:22,969 INFO  [STDOUT] [ERROR] 2012-08-13 23:55:22           LP::OfferController - #\)F3XjY_v: PAYKEY is blank or missing on item page
s1-sn701:2012-08-13 23:29:31,458 INFO  [STDOUT] [ERROR] 2012-08-13 23:29:31           LP::ThisController - #z|gXWQY1S: EAL: ASYNC: in async payment could not create items, api returned 320
s1-sn902:2012-08-13 12:40:13,350 INFO  [STDOUT] [ERROR] 2012-08-13 12:40:13           UI::Rails - #ErS;=x*'): Failed to get [1]https://aurl.url.com/v1/85/pp/accounting/  [2]betsy@betsyklein.com/
s1-sn902:2012-08-13 12:40:13,349 INFO  [STDOUT] [ERROR] 2012-08-13 12:40:13           UI::Rails - #ErS;=x*'): ["classpath:/META-INF/jruby.home/lib/ruby/1.8/uri/common.rb:436:in `split'"
s1-sn902:2012-08-13 12:40:13,347 INFO  [STDOUT] [ERROR] 2012-08-13 12:40:13           UI::Rails - #ErS;=x*'): -----------------------------
s1-sn902:2012-08-13 12:40:13,346 INFO  [STDOUT] [ERROR] 2012-08-13 12:40:13           UI::Rails - #ErS;=x*'): bad URI(is not URI?): [3]https://aurl.url.com/85/bills/pp/accounting/  [4]uname@aurl.com/
s1-sn902:2012-08-13 12:40:13,346 INFO  [STDOUT] [ERROR] 2012-08-13 12:40:13           UI::Rails - #ErS;=x*'): Oops, an error occured!
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Example of data I want to extract as the error field:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;EAL: ASYNC: in async
Error decoding or st
API returned 952 err
PAYKEY from payment 
PAYKEY from session 
Attempting to purcha
PAYKEY is blank or m
ASYNC: in async pay
Failed to get [1]htt
["classpath:/META-IN
--------------------
bad URI(is not URI?)
Oops, an error occur
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Aug 2012 15:05:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-capture-exactly-20-characters/m-p/34541#M7465</guid>
      <dc:creator>hharvey</dc:creator>
      <dc:date>2012-08-14T15:05:27Z</dc:date>
    </item>
    <item>
      <title>Re: Using regex to capture exactly 20 characters</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-capture-exactly-20-characters/m-p/34542#M7466</link>
      <description>&lt;P&gt;Hmm, that was a bit hard to read... &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;First do you NEED the full error messsage, otherwise you can just alter the rex to just capture up to 20 characters;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;rex "\#[\w0-9\W]{9}:\s(?P&amp;lt;ERROR&amp;gt;[^\\*]{1,20})"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Also, you could probably make it a bit easier on the eye like this;&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;rex "\#.{9}:\s(?P&amp;lt;ERROR&amp;gt;.{20})"
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;if the messages themselves are always more than 20 chars long.&lt;/P&gt;

&lt;P&gt;Hope this helps,&lt;/P&gt;

&lt;P&gt;Kristian&lt;/P&gt;</description>
      <pubDate>Tue, 14 Aug 2012 15:28:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-capture-exactly-20-characters/m-p/34542#M7466</guid>
      <dc:creator>kristian_kolb</dc:creator>
      <dc:date>2012-08-14T15:28:11Z</dc:date>
    </item>
    <item>
      <title>Re: Using regex to capture exactly 20 characters</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-capture-exactly-20-characters/m-p/34543#M7467</link>
      <description>&lt;P&gt;Thanks Kristian! adding {1,20} did it, I just didn't realize that was an option in regex.&lt;/P&gt;

&lt;P&gt;i agree, my post was pretty to read through. sorry!&lt;/P&gt;</description>
      <pubDate>Tue, 14 Aug 2012 15:46:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-capture-exactly-20-characters/m-p/34543#M7467</guid>
      <dc:creator>hharvey</dc:creator>
      <dc:date>2012-08-14T15:46:24Z</dc:date>
    </item>
    <item>
      <title>Re: Using regex to capture exactly 20 characters</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-capture-exactly-20-characters/m-p/34544#M7468</link>
      <description>&lt;P&gt;Well, you used that construct in the beginning - the {9} &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;
As you said EXACTLY 20 characters it's probably more correct to use {20} instead of {1,20} - but that's your decision.&lt;/P&gt;

&lt;P&gt;/k&lt;/P&gt;</description>
      <pubDate>Tue, 14 Aug 2012 15:57:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Using-regex-to-capture-exactly-20-characters/m-p/34544#M7468</guid>
      <dc:creator>kristian_kolb</dc:creator>
      <dc:date>2012-08-14T15:57:49Z</dc:date>
    </item>
  </channel>
</rss>

