<?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: How to write a search to use regex to extract multiple fields from a log line and find the total count of all matched patterns? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-use-regex-to-extract-multiple-fields/m-p/112917#M29668</link>
    <description>&lt;P&gt;Hi @sugitime&lt;/P&gt;

&lt;P&gt;Regex experts floating here on Answers will need you to provide some sample data and specify what parts you want to extract to really help you put the syntax together &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 14 Nov 2014 16:49:49 GMT</pubDate>
    <dc:creator>ppablo</dc:creator>
    <dc:date>2014-11-14T16:49:49Z</dc:date>
    <item>
      <title>How to write a search to use regex to extract multiple fields from a log line and find the total count of all matched patterns?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-use-regex-to-extract-multiple-fields/m-p/112916#M29667</link>
      <description>&lt;P&gt;I've looked through several of the other questions related to this one, but they were either unanswered, or answered with a workaround specific to the question asked.&lt;/P&gt;

&lt;P&gt;I am trying to write a search which will search all log lines in all indices, and grep for specific patterns. &lt;BR /&gt;
Each log line may have between 0 and all patterns that match.&lt;BR /&gt;
The logs format vary based on what they are (custom logs, apache logs, etc).&lt;BR /&gt;
I am not able to edit the transforms.conf; this would have to be done within the search line.&lt;BR /&gt;
What I am interested in is the total count of matched patterns.&lt;/P&gt;

&lt;P&gt;My first attempt was:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=* | regex("regexpattern1"="/regexpattern/") | regex("regexpattern2"="/regexpattern/") | regex("regexpattern3"="/regexpattern/")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;But I think that this is causing the results of one regex to just be piped into the other regex, which is not exactly what I was looking for.&lt;/P&gt;

&lt;P&gt;Could anyone provide me a bit of guidance as to where I might start looking for this?&lt;/P&gt;</description>
      <pubDate>Fri, 14 Nov 2014 16:38:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-use-regex-to-extract-multiple-fields/m-p/112916#M29667</guid>
      <dc:creator>sugitime</dc:creator>
      <dc:date>2014-11-14T16:38:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a search to use regex to extract multiple fields from a log line and find the total count of all matched patterns?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-use-regex-to-extract-multiple-fields/m-p/112917#M29668</link>
      <description>&lt;P&gt;Hi @sugitime&lt;/P&gt;

&lt;P&gt;Regex experts floating here on Answers will need you to provide some sample data and specify what parts you want to extract to really help you put the syntax together &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 14 Nov 2014 16:49:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-use-regex-to-extract-multiple-fields/m-p/112917#M29668</guid>
      <dc:creator>ppablo</dc:creator>
      <dc:date>2014-11-14T16:49:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a search to use regex to extract multiple fields from a log line and find the total count of all matched patterns?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-use-regex-to-extract-multiple-fields/m-p/112918#M29669</link>
      <description>&lt;P&gt;Without much information give this a try&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=* | eval type1=if(searchmatch("regexpattern1"="/regexpattern/") ,1,0)| eval type2=if(searchmatch("regexpattern2"="/regexpattern/"),1,0) | eval type3=if(searchmatch("regexpattern3"="/regexpattern/"),1,0) | stats count as Total Events sum(type1) as Type1Count sum(type2) as Type2Count sum(type3) as Type3Count
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 14 Nov 2014 17:00:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-use-regex-to-extract-multiple-fields/m-p/112918#M29669</guid>
      <dc:creator>somesoni2</dc:creator>
      <dc:date>2014-11-14T17:00:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a search to use regex to extract multiple fields from a log line and find the total count of all matched patterns?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-use-regex-to-extract-multiple-fields/m-p/112919#M29670</link>
      <description>&lt;P&gt;I can understand that. However I cant really provide samples. There are just so many different types of logs that could exist.&lt;/P&gt;

&lt;P&gt;I wrote a python script which goes to different log locations, opens each log, reads each log line and compares those log lines to regex patterns I have created. It records the results and moves on.&lt;/P&gt;

&lt;P&gt;I am trying to replace this with Splunk. So the log type really cant matter; the regex pattern would need to be able to look at each and every log line and match only on pattern, with no consideration to the type of file, the format of the file, the layout of the file or thing else related to the file itself.&lt;/P&gt;</description>
      <pubDate>Fri, 14 Nov 2014 17:08:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-use-regex-to-extract-multiple-fields/m-p/112919#M29670</guid>
      <dc:creator>sugitime</dc:creator>
      <dc:date>2014-11-14T17:08:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to write a search to use regex to extract multiple fields from a log line and find the total count of all matched patterns?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-use-regex-to-extract-multiple-fields/m-p/112920#M29671</link>
      <description>&lt;P&gt;I took a look at that. I got this error:&lt;BR /&gt;
Error in 'eval' command: The arguments to the 'searchmatch' function are invalid. &lt;/P&gt;

&lt;P&gt;I changed the searchmatch() to only use 1 argument (per the Splunk docs):&lt;BR /&gt;
eval type1=if(searchmatch("/regexpattern/") ,1,0)&lt;/P&gt;

&lt;P&gt;Then I piped those out to stats sum(type1) as Type1Count&lt;/P&gt;

&lt;P&gt;Then I did this for the rest of the patterns, and it seems to be working &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;
So my final search (thanks to your help) is:&lt;BR /&gt;
index=* | eval type1=if(searchmatch("/regexpattern/") ,1,0)| eval type2=if(searchmatch("/regexpattern/"),1,0) | eval type3=if(searchmatch("/regexpattern/"),1,0) | stats sum(type1) as Type1Count sum(type2) as Type2Count sum(type3) as Type3Count&lt;/P&gt;

&lt;P&gt;Thank you!!!&lt;/P&gt;</description>
      <pubDate>Fri, 14 Nov 2014 17:24:39 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-a-search-to-use-regex-to-extract-multiple-fields/m-p/112920#M29671</guid>
      <dc:creator>sugitime</dc:creator>
      <dc:date>2014-11-14T17:24:39Z</dc:date>
    </item>
  </channel>
</rss>

