Splunk Search

Combine Multiple Fields

JaysonD123
Explorer

Hello,

 

I'm relatively new to Splunk. I have multiple fields with different naming schemes that have different  or identical values. Here's an example:

 

hash=yj843yj387hfhjf723hjf47hnf29nf

hashes=xmv98svmd89djmfv98jvkfj9jm

Hashes=n9nuevur9vv9v8fj0fefjeffjv9ejve8

sha1_hash=84hmrh42mfu2hmxufxfmu28

src_hash=2xf9mf4jmfijjumrfx2r9mjfru2mjrm9j

 

name=jayson

Src_name=jayson

NAME=jayson

SubjectUserName=jayson

 

I'm trying to make a query checks if there is a field that contains the word "hash" or "name" and tables it out. Here's what I have so far:

 

| eval Hash=hash, Hash=hashes

| foreach Hash* [eval Hash=mvappend(Hash, "")]
| eval Name=name, Name=Src_name
| foreach Name* [eval Name=mvappend(Name, "")
| table Name Hash



I need to table the results from any field that has the word "hash" or "name" in it. Also is there a way to simplify this?

Labels (2)
1 Solution

bowesmana
SplunkTrust
SplunkTrust

@JaysonD123 

Good to see that you're diving in the deep end with the use of foreach, which is not such a commonly used command, but a very powerful one...

You're on the right track, but your initial assignments Hash=hash, Hash=hashes, will not do much good as it's making the field Hash equal to the field hashes, regardless of what hash is.

Fields are case sensitive, so from your sample data, you need to be doing a case insensitive comparison of the field name to either name or hashes. This runnable example shows you how to do this, also using foreach, but using the <<MATCHSTR>> and <<FIELD>> elements of foreach, which are crucial to getting this to work.

| makeresults
| eval hash="yj843yj387hfhjf723hjf47hnf29nf"
| eval hashes="xmv98svmd89djmfv98jvkfj9jm"
| eval Hashes="n9nuevur9vv9v8fj0fefjeffjv9ejve8"
| eval sha1_hash="84hmrh42mfu2hmxufxfmu28"
| eval src_hash="2xf9mf4jmfijjumrfx2r9mjfru2mjrm9j"
| eval name="jayson"
| eval Src_name="jayson"
| eval NAME="jayson"
| eval SubjectUserName="jayson"
| eval anotherField1="Something else", anotherField2="another"
| foreach * [ eval N=if(match("<<MATCHSTR>>","(?i)name"), mvappend("<<MATCHSTR>>=".<<FIELD>>, N), N),  H=if(match("<<MATCHSTR>>","(?i)hash"), mvappend("<<MATCHSTR>>=".<<FIELD>>, H), H)]

All you need is the  foreach statement. I have made it foreach *, so all field names can be checked. The H and N fields end up with a multi value field with fieldName=value for each of the matching fields.

Edit the mvappend as required.

 

View solution in original post

bowesmana
SplunkTrust
SplunkTrust

@JaysonD123 

Good to see that you're diving in the deep end with the use of foreach, which is not such a commonly used command, but a very powerful one...

You're on the right track, but your initial assignments Hash=hash, Hash=hashes, will not do much good as it's making the field Hash equal to the field hashes, regardless of what hash is.

Fields are case sensitive, so from your sample data, you need to be doing a case insensitive comparison of the field name to either name or hashes. This runnable example shows you how to do this, also using foreach, but using the <<MATCHSTR>> and <<FIELD>> elements of foreach, which are crucial to getting this to work.

| makeresults
| eval hash="yj843yj387hfhjf723hjf47hnf29nf"
| eval hashes="xmv98svmd89djmfv98jvkfj9jm"
| eval Hashes="n9nuevur9vv9v8fj0fefjeffjv9ejve8"
| eval sha1_hash="84hmrh42mfu2hmxufxfmu28"
| eval src_hash="2xf9mf4jmfijjumrfx2r9mjfru2mjrm9j"
| eval name="jayson"
| eval Src_name="jayson"
| eval NAME="jayson"
| eval SubjectUserName="jayson"
| eval anotherField1="Something else", anotherField2="another"
| foreach * [ eval N=if(match("<<MATCHSTR>>","(?i)name"), mvappend("<<MATCHSTR>>=".<<FIELD>>, N), N),  H=if(match("<<MATCHSTR>>","(?i)hash"), mvappend("<<MATCHSTR>>=".<<FIELD>>, H), H)]

All you need is the  foreach statement. I have made it foreach *, so all field names can be checked. The H and N fields end up with a multi value field with fieldName=value for each of the matching fields.

Edit the mvappend as required.

 

Got questions? Get answers!

Join the Splunk Community Slack to learn, troubleshoot, and make connections with fellow Splunk practitioners in real time!

Meet up IRL or virtually!

Join Splunk User Groups to connect and learn in-person by region or remotely by topic or industry.

Get Updates on the Splunk Community!

Quantify Your Splunk Investment Impact: Introducing Savings Metrics to Value Insights

Building on the foundation established in our initial Value Insights releases, we are introducing the Savings ...

Event Series: Telemetry Pipeline Management

Balancing Scale and Spend: Gaining Control Over High-Volume Metrics in Splunk Observability Cloud As ...

Kick the Tires Before You Commit: A Hands-On Tour of the Splunk Observability Cloud ...

Evaluating an enterprise observability platform usually goes like this: fill out a form, get a free trial with ...