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!

Extending Observability Content to Splunk Cloud

Watch Now!   In this Extending Observability Content to Splunk Cloud Tech Talk, you'll see how to leverage ...

More Control Over Your Monitoring Costs with Archived Metrics!

What if there was a way you could keep all the metrics data you need while saving on storage costs?This is now ...

New in Observability Cloud - Explicit Bucket Histograms

Splunk introduces native support for histograms as a metric data type within Observability Cloud with Explicit ...