Splunk Search

How to create strcat field names with spaces?

jlaska
Engager

I'm working with two similar, but not quite the same datasets and I want to create a table which displays data from either of the data sets in the same column.  For example, one dataset has the field Source_Name and the other dataset has the field "Source Name".  While I understand that spaces in field names is evil and should eb avoided, I have no control over the source data and am stuck working with what I have. 

In the past, when trying to mash two datasets together like this, I have relied on strcat to give me a consistent field name to work with ala: 

 

 

 

some search 
| strcat FieldA FieldB FieldAB

 

 

 

Since only FieldA or FieldB will be populated, based on which dataset the record is from, I will get the appropriate entry in the resultant FieldAB.  However, I've run into an issue this time as the spaces in the field name of one of my datasets is giving me trouble. I've tried:

 

 

 

| "Source Name" Source_Name SourceName

 

 

 

This treats the "Source Name" as a string literal.

 

 

 

| 'Source Name' Source_Name SourceName

 

 

 

This returns nothing for the case where 'Source Name' exists and 'Source_Name' does not.

 

 

 

| \"Source Name\" Source_Name SourceName

 

 

 

This returns nothing for the case where 'Source Name' exists and 'Source_Name' does not.

 

I do have a workaround where I rename the "Source Name" field to Source-Name and then do the strcat using that field.  This works; but, I am wondering if there is a cleaner solution?  Is there a way to reference a field name with a space in it, with the strcat command?  Alternatively, is there just a better way of selectively displaying a different field, depending on which one exists in a dataset, specifically at search time?

Labels (1)
0 Karma
1 Solution

richgalloway
SplunkTrust
SplunkTrust

I prefer the coalesce function for this scenario.  It populates a field with the first non-null argument provided.

| eval Field = coalesce(FieldA, FieldB)
---
If this reply helps you, Karma would be appreciated.

View solution in original post

richgalloway
SplunkTrust
SplunkTrust

I prefer the coalesce function for this scenario.  It populates a field with the first non-null argument provided.

| eval Field = coalesce(FieldA, FieldB)
---
If this reply helps you, Karma would be appreciated.

jlaska
Engager

That's perfect, thank you.

As a note for anyone else looking at this, for fields with spaces in the names, it's necessary to put the field name in single quotes.  E.g.:

| eval SourceName=coalesce('Source Name', Source_Name)
Get Updates on the Splunk Community!

Introducing Splunk Enterprise 9.2

WATCH HERE! Watch this Tech Talk to learn about the latest features and enhancements shipped in the new Splunk ...

Adoption of RUM and APM at Splunk

    Unleash the power of Splunk Observability   Watch Now In this can't miss Tech Talk! The Splunk Growth ...

Routing logs with Splunk OTel Collector for Kubernetes

The Splunk Distribution of the OpenTelemetry (OTel) Collector is a product that provides a way to ingest ...