Splunk Search

Set Field based on another field

axsolis
Path Finder

I am trying to create a field that contains information about the type of host based on the host field. For example, I have DMZ servers and Private servers. DMZ servers always have a hostname that ends in a "z" and Private servers always have a hostname that end in a "p". I would like to create a field to equal "DMZ" when the host ends in "z" and "Private" when host ends in "p".

There might be an easier way to do this but my ultimate goal is to timechart certain logs by location of servers. SO if I have 20 servers on the DMZ and 80 servers on the Private LAN all servers will be grouped by location in a timechart. (hope this make sense to somebody 🙂

example: ....|timechart count by LocationField

I am a Splunk newbie so please forgive me if I am going about this the wrong way.

Thanks.

1 Solution

Stephen_Sorkin
Splunk Employee
Splunk Employee

You can use the case statement in eval to do this:

... | eval LocationField = case(host LIKE "%p", "private", host LIKE "%z", "DMZ")

View solution in original post

sideview
SplunkTrust
SplunkTrust

This will do it.

<your search>  | eval hostType=if(substr(host,-1)=="z","DMZ","private")

Stephen_Sorkin
Splunk Employee
Splunk Employee

You can use the case statement in eval to do this:

... | eval LocationField = case(host LIKE "%p", "private", host LIKE "%z", "DMZ")

Stephen_Sorkin
Splunk Employee
Splunk Employee

I'd recommend making a search macro for this. You'd then invoke as: ... | setlocation | ...

0 Karma

axsolis
Path Finder

Thanks! that woked! Now is there a way to place this in some sort of function so I do not have to write the entire case() portion again? Or is there a way to make the LocationField permanent so LocationField will always come up in results?

Thanks for any help provided!

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 ...