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!

Analytics Workspace deprecation

As of Splunk Cloud Platform 10.4.2604 and Splunk Enterprise 10.4, Analytics Workspace is now deprecated. ...

Splunk Developer Day Recap: Building, Publishing, and Growing on the Splunk Platform

Splunk Developer Day brought the Splunk developer community together for a practical look at what it means to ...

[Puzzles] Solve, Learn, Repeat: Matching cron expressions

This puzzle (first published here) is based on matching timestamps to cron expressions.All the timestamps ...