Knowledge Management

Reference a calculated field within another calculated field

RWL01
Engager

Is it possible to reference a calculated field within another calculated field?

My original search query was:

| eval SourceServer=substr(source, 11, 6)
| eval SourceInstance=case(
     SourceServer=="AAA001", "PROD", 
     SourceServer=="AAA002", "PROD", 
     SourceServer=="BBB001", "UAT", 
     SourceServer=="BBB002", "UAT",
    1=1, "UNKNOWN")

My intention was to create two calculated fields. However, it doesn't look like SourceServer is processed before SourceInstance is, so all of the Events resolve to "UNKNOWN".

My alternative is to repeat the same logic for each option in the case statement which seems pretty inefficient.

SourceInstance

case(
     substr(source, 11, 6)=="AAA001", "PROD", 
     substr(source, 11, 6)=="AAA002", "PROD", 
     substr(source, 11, 6)=="BBB001", "UAT", 
     substr(source, 11, 6)=="BBB002", "UAT",
    1=1, "UNKNOWN")

When I try to chain them together in a single field, like the simplified example below I get the error "Encountered the following error while trying to update: Unexpected token"

| eval foo=4 | eval bar=foo+4

Is there anyway to accomplish the logic of my original query without having to repeat the first query for every option of the case statement in the second query?

Tags (1)
0 Karma

MuS
Legend

Hi RWL01,

there must be something wrong somewhere else, because both examples work fine here:

First one:

| makeresults 
| eval SourceServer=substr("ZZZZZZZZZZAAA001", 11, 6) 
| eval SourceInstance=case(
    SourceServer=="AAA001", "PROD", 
    SourceServer=="AAA002", "PROD", 
    SourceServer=="BBB001", "UAT", 
    SourceServer=="BBB002", "UAT",
    1=1, "UNKNOWN")

alt text

As well as the second one:

alt text

cheers, MuS

0 Karma
Get Updates on the Splunk Community!

Dashboards: Hiding charts while search is being executed and other uses for tokens

There are a couple of features of SimpleXML / Classic dashboards that can be used to enhance the user ...

Splunk Observability Cloud's AI Assistant in Action Series: Explaining Metrics and ...

This is the fourth post in the Splunk Observability Cloud’s AI Assistant in Action series that digs into how ...

Brains, Bytes, and Boston: Learn from the Best at .conf25

When you think of Boston, you might picture colonial charm, world-class universities, or even the crack of a ...