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
Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Best Practices: Splunk auto adjust pipeline queue

When you enable autoAdjustQueue in Splunk, maxSize should be understood as the queue size Splunk starts with ...

Laser Bananas and Edge Hubs: Exploring Operational Technology (OT) Data Through a ...

  OT is a different environment to traditional IT and can have interesting challenges when interfacing the ...

Event Series: Mastering AI Tokenomics and Splunk Agent Observability

Beyond the Black Box: Correlating AI Performance and Tokenomics with Splunk Agent Observability   As ...