Splunk Search

Check the first character of a string with eval case

Chandras11
Communicator

Hi Team,
I want to create a new field REGION_ID With following requrirements:-
If (TKT_CREATOR ="IP-Z" OR "DEP-IP-Z")
REGION_ID = "IpHW"
Else If (TKT_CREATOR ="IP-Z" OR "DEP-IP-Z")
REGION_ID = "IP-CS"

index="Index1" sourcetype="XXX"| eval REGION_ID = case((TKT_CREATOR == "IP-Z" OR TKT_CREATOR == "DEP-IP-Z"), "IpHW", (TKT_CREATOR== "IP-W" OR TKT_CREATOR == "DEP-IP-W"), "IP-CS")

and this is working fine.

Now I would also like to check If First Character of another field "Name" is "X" then REGION_ID = "X_Region" (in the same eval case statement). Is there a way to check the first character of a field value and assign other value to the new field REGION_ID.

0 Karma
1 Solution

mayurr98
Super Champion

hey Try this

index="Index1" sourcetype="XXX" 
| eval REGION_ID = case((TKT_CREATOR == "IP-Z" OR TKT_CREATOR == "DEP-IP-Z"), "IpHW", (TKT_CREATOR== "IP-W" OR TKT_CREATOR == "DEP-IP-W"), "IP-CS",substr(Name,1,1)="X","X_Region")

let me know if this helps!

View solution in original post

mayurr98
Super Champion

hey Try this

index="Index1" sourcetype="XXX" 
| eval REGION_ID = case((TKT_CREATOR == "IP-Z" OR TKT_CREATOR == "DEP-IP-Z"), "IpHW", (TKT_CREATOR== "IP-W" OR TKT_CREATOR == "DEP-IP-W"), "IP-CS",substr(Name,1,1)="X","X_Region")

let me know if this helps!

kmaron
Motivator

I didn't think to put the substr right in the case. I like that!

0 Karma

Chandras11
Communicator

super, thank you 🙂

0 Karma

kmaron
Motivator
index="Index1" sourcetype="XXX" 
| eval firstCharOfName=substr(Name,1,1) 
| eval REGION_ID = case((TKT_CREATOR == "IP-Z" OR TKT_CREATOR == "DEP-IP-Z"), "IpHW", (TKT_CREATOR== "IP-W" OR TKT_CREATOR == "DEP-IP-W"), "IP-CS", firstCharOfName=="X", "X_Region")

If you extract the first character of Name using an eval into a field firstCharofName then you should be able to add that to your case.

Get Updates on the Splunk Community!

Built-in Service Level Objectives Management to Bridge the Gap Between Service & ...

Wednesday, May 29, 2024  |  11AM PST / 2PM ESTRegister now and join us to learn more about how you can ...

Get Your Exclusive Splunk Certified Cybersecurity Defense Engineer at Splunk .conf24 ...

We’re excited to announce a new Splunk certification exam being released at .conf24! If you’re headed to Vegas ...

Share Your Ideas & Meet the Lantern team at .Conf! Plus All of This Month’s New ...

Splunk Lantern is Splunk’s customer success center that provides advice from Splunk experts on valuable data ...