<?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 conditional regex to extract field A, but if field A does not exist, then extract field B? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198607#M57455</link>
    <description>&lt;P&gt;try this: &lt;CODE&gt;[your search query] | rex _raw "15=(?P&amp;lt;currency_a&amp;gt;\w+)" | rex _raw "55=(?P&amp;lt;currency_b&amp;gt;\w+)" | eval return_value = if( isnull(currency_a), currency_b, currency_a)&lt;/CODE&gt; &lt;/P&gt;

&lt;P&gt;assuming the 'a' field is always preceded by '15=' and 'b' field is always preceded by '55='&lt;/P&gt;</description>
    <pubDate>Mon, 01 Sep 2014 14:03:24 GMT</pubDate>
    <dc:creator>sk314</dc:creator>
    <dc:date>2014-09-01T14:03:24Z</dc:date>
    <item>
      <title>How to write conditional regex to extract field A, but if field A does not exist, then extract field B?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198600#M57448</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;

&lt;P&gt;I have a problem in Splunk's regex and I can't figure it out for the life of me. &lt;/P&gt;

&lt;P&gt;I'm going to simplify my problem a bit. Imagine this is my data:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;|a|b|
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;If 'a' exists, I want my regex to pick out 'a' only, otherwise I want it to pick out 'b' only.&lt;/P&gt;

&lt;P&gt;So in this case: |a|b| my regex should pick out 'a'. But, if for some reason 'a' didn't exist in my data, I want &lt;STRONG&gt;the same bit of regex&lt;/STRONG&gt; to pick out 'b'.&lt;/P&gt;

&lt;P&gt;|a|b|  ---&amp;gt; should pick out 'a' only&lt;/P&gt;

&lt;P&gt;|c|b|  ---&amp;gt; should pick out 'b' only&lt;/P&gt;

&lt;P&gt;'b' always exists, but 'a' sometimes doesn't.&lt;/P&gt;

&lt;P&gt;Any ideas?&lt;/P&gt;

&lt;P&gt;Thanks a lot!&lt;/P&gt;

&lt;P&gt;Phil&lt;/P&gt;</description>
      <pubDate>Fri, 29 Aug 2014 16:05:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198600#M57448</guid>
      <dc:creator>philallen1</dc:creator>
      <dc:date>2014-08-29T16:05:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to write conditional regex to extract field A, but if field A does not exist, then extract field B?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198601#M57449</link>
      <description>&lt;P&gt;Hi @philallen1&lt;/P&gt;

&lt;P&gt;This is a great question. It'll be really helpful if you could paste a sample of your data so people can help you figure out an accurate regex for your use case.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Aug 2014 17:47:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198601#M57449</guid>
      <dc:creator>ppablo</dc:creator>
      <dc:date>2014-08-29T17:47:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to write conditional regex to extract field A, but if field A does not exist, then extract field B?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198602#M57450</link>
      <description>&lt;P&gt;Since "'b' always exists, but 'a' sometimes doesn't"&lt;BR /&gt;
Have you tried using fillnull on field 'a'? like so:&lt;/P&gt;

&lt;P&gt;[your search] | fillnull value=0 a | rex a "regex for field a" | rex b "regex for field b" | eval return_value = if(isnull(a), b, a)&lt;/P&gt;</description>
      <pubDate>Sat, 30 Aug 2014 12:02:01 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198602#M57450</guid>
      <dc:creator>sk314</dc:creator>
      <dc:date>2014-08-30T12:02:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to write conditional regex to extract field A, but if field A does not exist, then extract field B?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198603#M57451</link>
      <description>&lt;P&gt;Why does it have to be "the same bit of regex"??&lt;/P&gt;</description>
      <pubDate>Sun, 31 Aug 2014 05:57:03 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198603#M57451</guid>
      <dc:creator>lguinn2</dc:creator>
      <dc:date>2014-08-31T05:57:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to write conditional regex to extract field A, but if field A does not exist, then extract field B?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198604#M57452</link>
      <description>&lt;P&gt;You question clearly states that a and b are two separate fields, but your example shows that a is value of a field. &lt;/P&gt;

&lt;P&gt;Assuming you will have a and b as two distinct fields, you need coalesce function.&lt;/P&gt;

&lt;P&gt;try like this:&lt;BR /&gt;
Your base search...| eval RequiredField = coalesce(a,b)&lt;/P&gt;

&lt;P&gt;Read more on coalesce here: &lt;A href="http://docs.splunk.com/Documentation/Splunk/6.1.3/SearchReference/Commonevalfunctions"&gt;http://docs.splunk.com/Documentation/Splunk/6.1.3/SearchReference/Commonevalfunctions&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;As @ppablo_splunk mentioned, if you can give sample log events, it will be helpful for us to answer your question.&lt;/P&gt;</description>
      <pubDate>Sun, 31 Aug 2014 17:45:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198604#M57452</guid>
      <dc:creator>strive</dc:creator>
      <dc:date>2014-08-31T17:45:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to write conditional regex to extract field A, but if field A does not exist, then extract field B?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198605#M57453</link>
      <description>&lt;P&gt;Hi guys&lt;/P&gt;

&lt;P&gt;Thanks for the responses. Here are some sample logs.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;2014-08-29 16:09:00,830 INFO Order filled for 8=FIX.4.4|9=667|15=GBP|55=USD

2014-08-27 16:11:19,373 INFO Order filled for 8=FIX.4.4|9=667|55=USD
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;So the fields I am referring to are "15=..." and "55=...". I'm interested in the currency that each of these fields give. &lt;/P&gt;

&lt;P&gt;The "15=..." field is the equivalent to my "a" field - i.e. it doesn't always exist. When it does exist I want the currency that it gives, otherwise I want the currency that "55=..." gives. &lt;/P&gt;

&lt;P&gt;I hope that makes sense?&lt;/P&gt;

&lt;P&gt;Phil&lt;/P&gt;</description>
      <pubDate>Mon, 01 Sep 2014 09:04:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198605#M57453</guid>
      <dc:creator>philallen1</dc:creator>
      <dc:date>2014-09-01T09:04:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to write conditional regex to extract field A, but if field A does not exist, then extract field B?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198606#M57454</link>
      <description>&lt;P&gt;I'm not sure if I need to put these @ signs in to get people's attention, so I'm giving it a go!&lt;BR /&gt;
@ppablo_splunk&lt;BR /&gt;
@sk314&lt;BR /&gt;
@lguinn&lt;BR /&gt;
@strive&lt;/P&gt;</description>
      <pubDate>Mon, 01 Sep 2014 10:43:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198606#M57454</guid>
      <dc:creator>philallen1</dc:creator>
      <dc:date>2014-09-01T10:43:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to write conditional regex to extract field A, but if field A does not exist, then extract field B?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198607#M57455</link>
      <description>&lt;P&gt;try this: &lt;CODE&gt;[your search query] | rex _raw "15=(?P&amp;lt;currency_a&amp;gt;\w+)" | rex _raw "55=(?P&amp;lt;currency_b&amp;gt;\w+)" | eval return_value = if( isnull(currency_a), currency_b, currency_a)&lt;/CODE&gt; &lt;/P&gt;

&lt;P&gt;assuming the 'a' field is always preceded by '15=' and 'b' field is always preceded by '55='&lt;/P&gt;</description>
      <pubDate>Mon, 01 Sep 2014 14:03:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198607#M57455</guid>
      <dc:creator>sk314</dc:creator>
      <dc:date>2014-09-01T14:03:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to write conditional regex to extract field A, but if field A does not exist, then extract field B?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198608#M57456</link>
      <description>&lt;P&gt;I would try something like &lt;/P&gt;

&lt;P&gt;... | rex "| \d+=(?&lt;CURRENCY&gt;[A-Z]+)"&lt;/CURRENCY&gt;&lt;/P&gt;

&lt;P&gt;In theory this will see the pipe followed by a number, equal sign, and then upper case letters and only do that for the first time it runs into that pattern. I forget if all currency has a 3 letter designate but you could potentially change the [A-Z]+ part to [A-Z]{3}. &lt;/P&gt;

&lt;P&gt;Am going off the top of my head with no caffeine though so YMMV.&lt;/P&gt;</description>
      <pubDate>Tue, 02 Sep 2014 10:07:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198608#M57456</guid>
      <dc:creator>Runals</dc:creator>
      <dc:date>2014-09-02T10:07:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to write conditional regex to extract field A, but if field A does not exist, then extract field B?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198609#M57457</link>
      <description>&lt;P&gt;Probably better (definitely simpler) to do &lt;CODE&gt;| eval return_value = coalesce(currency_a, currency_b)&lt;/CODE&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Feb 2016 23:54:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198609#M57457</guid>
      <dc:creator>David</dc:creator>
      <dc:date>2016-02-11T23:54:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to write conditional regex to extract field A, but if field A does not exist, then extract field B?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198610#M57458</link>
      <description>&lt;P&gt;Worked perfectly for my situation&lt;/P&gt;</description>
      <pubDate>Fri, 30 Nov 2018 23:06:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198610#M57458</guid>
      <dc:creator>sbotharaj</dc:creator>
      <dc:date>2018-11-30T23:06:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to write conditional regex to extract field A, but if field A does not exist, then extract field B?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198611#M57459</link>
      <description>&lt;P&gt;Here. &lt;CODE&gt;_&lt;/CODE&gt; fields are a little tricky so I would &lt;CODE&gt;eval&lt;/CODE&gt;/&lt;CODE&gt;rename&lt;/CODE&gt; them like I did here. &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=myindex | 

eval no_referrer_regex="MYREGEX1" |

eval referrer_regex="MYREGEX2" |

eval regex=if(_time &amp;lt; 1579250700,no_referrer_regex,referrer_regex) | eval raw=_raw |

map maxsearches=10000 search="| makeresults | eval mapped_raw=\"$$raw$$\" | rex field=mapped_raw \"$$regex$$\"" | table pst pst_epoch id action path num desc browser referrer
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;A second approach would just be to use ad-hoc searches in SimpleXML to set token values.&lt;/P&gt;</description>
      <pubDate>Fri, 17 Jan 2020 22:27:11 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198611#M57459</guid>
      <dc:creator>nick405060</dc:creator>
      <dc:date>2020-01-17T22:27:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to write conditional regex to extract field A, but if field A does not exist, then extract field B?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198612#M57460</link>
      <description>&lt;PRE&gt;&lt;CODE&gt;| makeresults 
| eval _raw="2014-08-29 16:09:00,830 INFO Order filled for 8=FIX.4.4|9=667|15=GBP|55=USD
2014-08-27 16:11:19,373 INFO Order filled for 8=FIX.4.4|9=667|55=USD" 
| makemv delim="
" _raw 
| stats count by _raw 
`comment("this is sample log")`
`comment("the logic")`
| rex "^(?&amp;lt;_time&amp;gt;[\w-]+ \d\d:\d\d:\d\d,\d{3}) .+ for (?&amp;lt;message&amp;gt;.+)" 
| eval _time=strptime(_time,"%F %T")
| eval message=split(message,"|")
| stats count by _time message
| rex field=message "(?&amp;lt;key&amp;gt;\w+)=(?&amp;lt;value&amp;gt;.+$)"
| eval {key} = value
| fields - message count key value
| stats values(*) as * by _time
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This is @philallen1 's  sample.&lt;BR /&gt;
I think it is unnecessary to use &lt;EM&gt;conditional REGEX&lt;/EM&gt; .&lt;/P&gt;</description>
      <pubDate>Sat, 18 Jan 2020 04:21:16 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198612#M57460</guid>
      <dc:creator>to4kawa</dc:creator>
      <dc:date>2020-01-18T04:21:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to write conditional regex to extract field A, but if field A does not exist, then extract field B?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198613#M57461</link>
      <description>&lt;P&gt;Try this (I may be taking you too literally, but the pieces that you need are here):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;(?J)(?:^|[\r\n])\|(?:(?&amp;lt;MyField&amp;gt;a)\|b\|)|(?:c\|(?&amp;lt;MyField&amp;gt;b)\|)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;&lt;A href="https://regex101.com/r/5Peq78/1"&gt;https://regex101.com/r/5Peq78/1&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Jan 2020 22:32:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-write-conditional-regex-to-extract-field-A-but-if-field-A/m-p/198613#M57461</guid>
      <dc:creator>woodcock</dc:creator>
      <dc:date>2020-01-19T22:32:27Z</dc:date>
    </item>
  </channel>
</rss>

