Splunk Search

How to write the regex for a calculated field to parse out certain text from my source field?

aelluru
New Member

I have an existing field named source which has a sample format of:

/home/user/script.schema.table.date-time.log 

How can I write the regex to parse out schema ? (i.e parse out text between first and second . )
How can I write the regex to parse out table? (i.e parse out text between second and third . )

0 Karma

somesoni2
SplunkTrust
SplunkTrust

This should do it

In-line in search

...| rex field=source "\/home\/([^\/]+)\/([^\.]+)\.(?<schema>[^\.]+)\.(?<table>[^\.]+)\."

In Props.conf (as calculated field)

[YourSourceType]
EVAL-schema = replace(source,"(\/home\/[^\/]+\/[^\.]+\.)([^\.]+)(\.[^\.]+)(.*)","\2")
EVAL-table= replace(source,"(\/home\/[^\/]+\/[^\.]+\.)([^\.]+)(\.[^\.]+)(.*)","\3")

In props.conf and transforms.conf (as field extraction)

props.conf

[YourSourceType]
TRANSFORMS-getfieldsfromsource = fieldsfromsource

transforms.conf

[fieldsfromsource]
SOURCE_KEY=MetaData:Source
REGEX=\/home\/([^\/]+)\/([^\.]+)\.(?<schema>[^\.]+)\.(?<table>[^\.]+)\.
0 Karma

pgrantham_splun
Splunk Employee
Splunk Employee

I believe something like this should work:

/home/\w+/[^\.]+\.(?<schema>[^\.]+)\.(?<table>[^\.]+)
0 Karma

sundareshr
Legend

This will give you two fields seg1 with schema and seg2 with table

... | rex field=source "[^\.]+\.(?<seg1>[^\.]+)\.(?<seg2>[^\.]+)\.") | table seg1 seg2
0 Karma
Get Updates on the Splunk Community!

Index This | I am a number, but when you add ‘G’ to me, I go away. What number am I?

March 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

What’s New in Splunk App for PCI Compliance 5.3.1?

The Splunk App for PCI Compliance allows customers to extend the power of their existing Splunk solution with ...

Extending Observability Content to Splunk Cloud

Register to join us !   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to ...