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!

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 ...

Get Inspired! We’ve Got Validation that Your Hard Work is Paying Off

We love our Splunk Community and want you to feel inspired by all your hard work! Eric Fusilero, our VP of ...

What's New in Splunk Enterprise 9.4: Features to Power Your Digital Resilience

Hey Splunky People! We are excited to share the latest updates in Splunk Enterprise 9.4. In this release we ...