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!

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...

Introducing the 2024 Splunk MVPs!

We are excited to announce the 2024 cohort of the Splunk MVP program. Splunk MVPs are passionate members of ...

Splunk Custom Visualizations App End of Life

The Splunk Custom Visualizations apps End of Life for SimpleXML will reach end of support on Dec 21, 2024, ...