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!

Enterprise Security Content Update (ESCU) | New Releases

In December, the Splunk Threat Research Team had 1 release of new security content via the Enterprise Security ...

Why am I not seeing the finding in Splunk Enterprise Security Analyst Queue?

(This is the first of a series of 2 blogs). Splunk Enterprise Security is a fantastic tool that offers robust ...

Index This | What are the 12 Days of Splunk-mas?

December 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...