Splunk Search

How to insert IF in regular expression

andreac81
Explorer

Hi to all, I should extract some fields by a log file, in the log file in some cases I have a field (i.e. field1, in other cases I have another field (i.e. field2).

For example the log file can contain field1=value1 or field2=value2

I need to do something like

IF log contains filed1 
EXTRACT- field1 =.*field1=(?P<field1>\w+).*
ELSE
EXTRACT- field2 =.*field2=(?P<field2>\w+).*

Is it possible?
Thanks,
Andrea

Tags (1)
0 Karma

alemarzu
Motivator

Hi there, perhaps something like this might work for you.

In props.conf add this:

[<your_sourcetype>]
TRANSFORMS-kv_xtraction = kv_extraction

In transforms.conf add this:

[kv_extraction]
REGEX = \s([^\s]+)=([^\s]+)
FORMAT = $1::$2

Hope it helps.

0 Karma

somesoni2
Revered Legend

The values seems to be standard key-value pair with equal sign as delimiter. Is Splunk not extracting them automatically for you (try to run the search in Smart Mode)?

Also, an event will contain either of field1 or field2, so do you want to extract them as separate field name (field1 or field2) or just a common name (say fieldname) so that you can correlate them? If you can use a common name, try like this

[yoursourcetype]
EXTRACT-fieldname = (field1|field2)=(?<fieldname>\w+)
0 Karma

andreac81
Explorer

Using
EXTRACT-fieldname = (field1|field2)=(?\w+)
I store both field1 or field2 in fieldname, I need to store field1 in fieldname1 and field2 in fieldname2 considering that the log file can contains sometimes field1 other times field2

Thanks

0 Karma

somesoni2
Revered Legend

Is Splunk not extracting them for you automatically? can we have some sample events which contains field1 and field2 ?

A simpler option would be to just setup two field extractions, one for field1 and one for field2. For events where field2 is not there, it won't extract anything anyways (null value).

[yoursourcetype]
 EXTRACT-field1= field1=(?<field1>\w+)
 EXTRACT-field2= field2=(?<field2>\w+)
Get Updates on the Splunk Community!

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...