Splunk Search

split the filed with a hyphen "-" separator

Allampally
Path Finder

Hi Team,

I have a index below and i want to split the index values and create a new field with it.
Example
index=app-production
index=app-sit
index=app-uat.... etc.
i want to create a new filed as "Environment" and add the splitted values to it such as
Environment field should have "production, sit, uat, .." values

Tags (1)
0 Karma
1 Solution

kamlesh_vaghela
SplunkTrust
SplunkTrust

@Allampally

Can you please try this?

YOUR_SEARCH | eval Environment=mvindex(split(index,"-"),1)

Sample search:

| makeresults | eval index="app-production" | eval Environment=mvindex(split(index,"-"),1)

View solution in original post

0 Karma

DavidHourani
Super Champion

Hi @Allampally,

Both solutions here from @kamlesh_vaghela and @koshyk will do what you're looking for. If you need a more permanent solution please add the following to your sourcetype in props.conf:

[yoursourcetype]
EVAL-Environment=mvindex(split(index,"-"),1)

This will always include the Environment search in all your queries without having to append anything to your search.

Cheers,
David

0 Karma

koshyk
Super Champion

alternatively you could use rex command too

 | makeresults | eval index="app-production" | rex field=index "\w+\-(?<Environment>\w+)"

cheers

0 Karma

kamlesh_vaghela
SplunkTrust
SplunkTrust

@Allampally

Can you please try this?

YOUR_SEARCH | eval Environment=mvindex(split(index,"-"),1)

Sample search:

| makeresults | eval index="app-production" | eval Environment=mvindex(split(index,"-"),1)
0 Karma
Get Updates on the Splunk Community!

Combine Multiline Logs into a Single Event with SOCK - a Guide for Advanced Users

This article is the continuation of the “Combine multiline logs into a single event with SOCK - a step-by-step ...

Everything Community at .conf24!

You may have seen mention of the .conf Community Zone 'round these parts and found yourself wondering what ...

Index This | I’m short for "configuration file.” What am I?

May 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with a Special ...